Ad

Monday, 9 March 2015

BeanCurrentlyInCreationException: Error creating bean with name : Requested bean is currently in creation: Is there an unresolvable circular reference?

In this post we will see how to resolve this exception-"BeanCurrentlyInCreationException: Error creating bean with name : Requested bean is currently in creation: Is there an unresolvable circular reference?" in spring.This exception is thrown in the below scenario:
  1. When there is an circular reference between the beans. For instance ClassA contains the instance member of type ClassB and class B too has ClassA as instance member. 
  2. And we use the constructor arguments to inject the dependencies between these beans.
Below code sample will explain this scenario.
  • ClassA,ClassB- bean classes for which there is an  circular dependencies between them.
  • CircularDemo -Main class which will load and initialize the spring container.
  • circontext.xml - metadata configuration details for the spring container.
ClassA.java

package test;

public class ClassA {

private ClassB classB=null;

public ClassB getClassB() {
return classB;
}

public void setClassB(ClassB classB) {
this.classB = classB;
}

@Override
public String toString() {
return "ClassA";
}

}

ClassB.java

package test;

public class ClassB {

private ClassA classA=null;

public ClassA getClassA() {
return classA;
}

public void setClassA(ClassA classA) {
this.classA = classA;
}

@Override
public String toString() {
return "ClassB";
}
}

circontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="classA" class="test.ClassA">
<constructor-arg ref="classB"/>
</bean>

<bean id="classB" class="test.ClassB">
<constructor-arg ref="classA"/>
</bean>

</beans>

As we can see in the application context file,we used the constructor arguments to inject the dependencies for bean ClassA and ClassB

CircularDemo.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CircularDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext bf= new ClassPathXmlApplicationContext(new String[]
               {"circontext.xml"});
ClassA classA= bf.getBean("classA",ClassA.class);
   System.out.println("Bean Loaded:"+classA);
}
}

OUTPUT:

When we run the main program - CircularDemo,we will get the below exception

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classA' defined in class path resource [circontext.xml]: Cannot resolve reference to bean 'classB' while setting constructor argument; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classB' defined in class path resource [circontext.xml]: Cannot resolve reference to bean 'classA' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'classA': Requested bean is currently in creation: Is there an unresolvable circular reference?

To resolve this exception,declare the dependencies between the beans through the property setters instead of the constructor arguments as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="classA" class="test.ClassA">
<property name="classB" ref="classB"/>
</bean>

<bean id="classB" class="test.ClassB">
<property name="classA" ref="classA"/>
</bean>

</beans>

Now if we run the CircularDemo with the above application context file,we will get the expected output.

Bean Loaded:ClassA

No comments:

Post a Comment