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

时间:2016-09-01 14:54:27

标签: java spring

最初我像这样初始化了对象

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()作为参数初始化的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

您必须创建一个范围为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" />

答案 1 :(得分:0)

有几种不同的方法可以做到这一点。由于您使用的是@Autowired注释,请使用@Value注释。

为了说明它在您的示例中的用法,您可以执行以下操作:

@Component
public class Stage {
    @Autowired
    public Stage(ScreenViewport screenViewport){
        // do stuff
    }
}

@Component
public class ScreenViewport {
    public ScreenViewport(@Value("#{some expression here}") String foo){
         // do even more stuff
    }
}

documentation中有更多信息。如果您还需要多个ScreenViewport个实例,请查看@Qualifier