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!
This is most certainly a circular dependency. You should be seeing a
BeanCurrentlyInCreationExceptionthrown 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.
DiameterClientI 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 yourxxxHandlerclasses. You should even be able to@AutowiredaList<DiameterMessageHandler>, and Spring will load allbeans that implement that interface into the list.constructor-argelement, use thepropertyelement).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 (
@Componentmost likely) and do acontext:component-scanon the packages that contain these classes. That would mean even less xml declaration.