java – 使用对象的新实例初始化bean

本来我初始化这样的对象

Stage stage = new Stage(new ScreenViewport())

哪里

stage -> com.badlogic.gdx.scenes.scene2d.Stage
screenViewport -> com.badlogic.gdx.utils.viewport.ScreenViewport

然后我决定用春天.
我在我的对象(@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?

使用新的ScreenViewport()作为参数初始化的正确方法是什么?

最佳答案 您必须使用范围原型创建bean screenViewPort.


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" />
点赞