Spring MVC @Autowire 不工作 - 循环依赖?
这是我的 xml 配置:
<bean id="diameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start">
<constructor-arg index="0"><value>${pcca.host}</value></constructor-arg>
<constructor-arg index="1"><value>${pcca.port}</value></constructor-arg>
<constructor-arg index="2" value="com.rory.djgx.message"/>
<constructor-arg index="3" value="com.rory.djgx.avp"/>
<constructor-arg index="4">
<list>
<ref bean="asrHandler"/>
<ref bean="aaaHandler"/>
<ref bean="ceaHandler"/>
<ref bean="dwaHandler"/>
<ref bean="staHandler"/>
</list>
</constructor-arg>
</bean>
<bean id="asrHandler" class="com.rory.ptspsim.messagereceivers.ASRHandler"/>
<bean id="aaaHandler" class="com.rory.ptspsim.messagereceivers.AAAHandler"/>
<bean id="ceaHandler" class="com.rory.ptspsim.messagereceivers.CEAHandler"/>
<bean id="dwaHandler" class="com.rory.ptspsim.messagereceivers.DWAHandler"/>
<bean id="staHandler" class="com.rory.ptspsim.messagereceivers.STAHandler"/>
这是 ASRHandler 类的开始:
public class ASRHandler implements DiameterMessageHandler
{
@Autowired
private DiameterClient diameterClient;
有谁知道为什么这不起作用?我意识到处理程序类和 DiameterClient 类相互依赖,但我认为 Spring 已经解决了这个问题。
以下是来自日志的消息:
org.springframework.beans.factory.BeanCreationException: 创建 ServletContext 资源中定义的名为“diameterClient”的 bean 时出错 [/WEB-INF/applicationContext.xml]: 无法解析对 bean“asrHandler”的引用使用键 [0] 设置构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“asrHandler”的 bean 时出错:资源依赖项注入失败;嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“diameterClient”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?
谢谢!
Here is my xml config:
<bean id="diameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start">
<constructor-arg index="0"><value>${pcca.host}</value></constructor-arg>
<constructor-arg index="1"><value>${pcca.port}</value></constructor-arg>
<constructor-arg index="2" value="com.rory.djgx.message"/>
<constructor-arg index="3" value="com.rory.djgx.avp"/>
<constructor-arg index="4">
<list>
<ref bean="asrHandler"/>
<ref bean="aaaHandler"/>
<ref bean="ceaHandler"/>
<ref bean="dwaHandler"/>
<ref bean="staHandler"/>
</list>
</constructor-arg>
</bean>
<bean id="asrHandler" class="com.rory.ptspsim.messagereceivers.ASRHandler"/>
<bean id="aaaHandler" class="com.rory.ptspsim.messagereceivers.AAAHandler"/>
<bean id="ceaHandler" class="com.rory.ptspsim.messagereceivers.CEAHandler"/>
<bean id="dwaHandler" class="com.rory.ptspsim.messagereceivers.DWAHandler"/>
<bean id="staHandler" class="com.rory.ptspsim.messagereceivers.STAHandler"/>
And here is the start of the ASRHandler class:
public class ASRHandler implements DiameterMessageHandler
{
@Autowired
private DiameterClient diameterClient;
Does anyone have any idea why this isnt working? I realise the the handler class and the DiameterClient class have a dependancy on each other, but I though Spring took care of that.
Here is the message from the log:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'diameterClient' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'asrHandler' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'asrHandler': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'diameterClient': Requested bean is currently in creation: Is there an unresolvable circular reference?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这肯定是一个循环依赖。您应该会看到 Spring 抛出的
BeanCurrentlyInCreationException
。当 Bean 已经被实例化时,就无法对其进行引用。问题是您正在使用构造函数注入来创建 bean。
我看到有两件事应该能够解决这个问题。
DiameterClient
的包名称,我假设它是您控制的一个类。我建议像使用xxxHandler
类一样自动装配该类的所有依赖项。您甚至应该能够@Autowired
一个List
,Spring会将所有实现该接口的bean
加载到列表中。property
元素,而不是constructor-arg
元素)。编辑:
如果您的文件看起来像这样:
和这样:
那么
List
将包含您定义的5个bean在 xml 中的diameterClient bean 下。如果顺序很重要,您可能需要在上下文中以特定顺序指定它们,但否则它应该可以工作。你甚至可以更进一步,使用 Stereotype 注释(最有可能的是
@Component
)来注释您的类,并对包含这些类的包执行context:component-scan
。这意味着更少的 xml 声明。This is most certainly a circular dependency. You should be seeing a
BeanCurrentlyInCreationException
thrown by Spring.Beans cannot be referenced when they are being instantiated already. The issue is that you are using constructor injection to create your beans.
I see two things that should be able to fix this.
DiameterClient
I am assuming it is a class that you are in control of. I would recommend autowiring all dependencies of that class as you have with yourxxxHandler
classes. You should even be able to@Autowired
aList<DiameterMessageHandler>
, and Spring will load allbean
s that implement that interface into the list.constructor-arg
element, use theproperty
element).EDIT:
If you have your files looks something like this:
and this:
then the
List<DiameterMessageHandler>
would contain the 5 beans you have defined under your diameterClient bean in your xml. If order is important, you may need to specify them in your context in a specific order, but otherwise it should just work.You could even take this further and annotate your classes with a Stereotype annotation (
@Component
most likely) and do acontext:component-scan
on the packages that contain these classes. That would mean even less xml declaration.您是否使用 @Component @Service、@Repository 或 @Controller 注释来注释您的类,实际上忘记了当 Diameter 客户端位于 ASRHandler 中时,您正在将 ASRHandler 连接到 Diameter 客户端,这是循环的,asr 是否这样做?处理程序需要自己的直径客户端吗?
Have you annotated your class with either a @Component @Service, @Repository or @Controller annotation, actually forget that you're wiring in a ASRHandler into the Diameter Client when the Diameter Client is in the ASRHandler, this is circular, does the asr handler need its own diameter client?