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

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

最佳答案

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

关于java - 使用对象的新实例初始化 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274843/

相关文章:

java - ajax 搜索在空格键后不起作用

java - 如何链接两个不同类的对象?

spring - spring 3.1.2 中使用注释的国际化不起作用

java - 将服务注入(inject)自定义 Jackson 序列化器

java - 为什么在 Java 中尝试反转字符串时会出现类型不匹配的情况?

java - 通过 Scanner 输入在 Java 中获取多个值

java - JSP 正在下载而不是渲染

java - Spring 数据休息,url 参数中的 java.util.Date?

java - 这是集成测试还是单元测试? (在 Spring 测试休息 Controller )

java - 微服务架构中的Hibernate Join表