本来我是这样初始化对象的
Stage stage = new Stage(new ScreenViewport())
哪里
stage -> com.badlogic.gdx.scenes.scene2d.Stage
screenViewport -> com.badlogic.gdx.utils.viewport.ScreenViewport
然后,我决定使用
spring
。我向我的对象(
@component
)添加了字段@Autowired
private Stage stage
并添加在
xml
中<bean id="stage"
class="com.badlogic.gdx.scenes.scene2d.Stage">
<constructor-arg type="com.badlogic.gdx.utils.viewport.ScreenViewport"
value="#{ new com.badlogic.gdx.utils.viewport.ScreenViewport() }"/>
</bean>
但是我得到这个例外
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationMain': Unsatisfied dependency expressed through field 'screen': Error creating bean with name 'applicationMenuScreen': Unsatisfied dependency expressed through field 'stage': Error creating bean with name 'stage' defined in class path resource [application-context.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [com.badlogic.gdx.utils.viewport.Viewport] - did you specify the correct bean references as arguments?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stage' defined in class path resource [application-context.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [com.badlogic.gdx.utils.viewport.Viewport] - did you specify the correct bean references as arguments?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationMenuScreen': Unsatisfied dependency expressed through field 'stage': Error creating bean with name 'stage' defined in class path resource [application-context.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [com.badlogic.gdx.utils.viewport.Viewport] - did you specify the correct bean references as arguments?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stage' defined in class path resource [application-context.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [com.badlogic.gdx.utils.viewport.Viewport] - did you specify the correct bean references as arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stage' defined in class path resource [application-context.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [com.badlogic.gdx.utils.viewport.Viewport] - did you specify the correct bean references as arguments?
用
new ScreenViewport()
作为参数初始化的正确方法是什么? 最佳答案
您必须使用范围screenViewPort
创建一个bean prototype
。
参见http://www.tutorialspoint.com/spring/spring_bean_scopes.htm
<bean id="stage"
class="com.badlogic.gdx.scenes.scene2d.Stage">
<property name="screenViewPort" ref="screenViewPort" />
</bean>
<bean id="screenViewPort" scope="prototype"
class="com.badlogic.gdx.utils.viewport.ScreenViewport" />