gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:30 26 4
gpt4 key购买 nike

原来我是这样初始化对象的

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com