Spring MVC @Autowire 不工作 - 循环依赖?

发布于 2025-01-08 16:28:28 字数 1958 浏览 0 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2025-01-15 16:28:28

这肯定是一个循环依赖。您应该会看到 Spring 抛出的 BeanCurrentlyInCreationException

当 Bean 已经被实例化时,就无法对其进行引用。问题是您正在使用构造函数注入来创建 bean。

我看到有两件事应该能够解决这个问题。

  1. 根据 DiameterClient 的包名称,我假设它是您控制的一个类。我建议像使用 xxxHandler 类一样自动装配该类的所有依赖项。您甚至应该能够@Autowired一个List,Spring会将所有实现该接口的bean加载到列表中。
  2. 在您的 Bean 上使用基于属性的注入(使用 property 元素,而不是 constructor-arg 元素)。

编辑:

如果您的文件看起来像这样:

<bean id="diameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start"/>

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

和这样:

public class DiameterClient {
    @Autowired
    private List<DiameterMessageHandler> handlers;

    @Value("${pcca.host}")
    private String host;

    @Value("${pcca.port}")
    private int port; // or String...

    // I don't know what these other two would be...if they are autowired, or just values you would populate...
    private String somePackage;

    private String anotherPackage;

    // methods go here...
}

那么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.

  1. Based on the package name of 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 your xxxHandler classes. You should even be able to @Autowired a List<DiameterMessageHandler>, and Spring will load all beans that implement that interface into the list.
  2. Use property based injection on your beans (instead of the constructor-arg element, use the property element).

EDIT:

If you have your files looks something like this:

<bean id="diameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start"/>

<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 this:

public class DiameterClient {
    @Autowired
    private List<DiameterMessageHandler> handlers;

    @Value("${pcca.host}")
    private String host;

    @Value("${pcca.port}")
    private int port; // or String...

    // I don't know what these other two would be...if they are autowired, or just values you would populate...
    private String somePackage;

    private String anotherPackage;

    // methods go here...
}

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 a context:component-scan on the packages that contain these classes. That would mean even less xml declaration.

眼波传意 2025-01-15 16:28:28

您是否使用 @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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文