0 votes
2.4k views
in Spring by

What is circular dependency in spring and how to solve it?

package com.circularBean.problem;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class A{
 
private B beanB;
 
@Autowired
public A(B beanB) {
this.beanB = beanB;
}
 
@Override
public String toString() {
return beanB.getClass().getSimpleName() + " from " + this.getClass().getSimpleName();
}
 
}
 
package com.circularBean.problem;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class B {
 
private A beanA;
 
@Autowired
public B(A beanA) {
this.beanA = beanA;
}
 
@Override
public String toString() {
return beanA.getClass().getSimpleName() + " from " + this.getClass().getSimpleName();
 
}
 
}
 
package com.circularBean.problem;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class CircularBeanMain {
 
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "resources/app-context.xml" });
A beanA = applicationContext.getBean(A.class);
B beanB = applicationContext.getBean(B.class);
System.out.println(beanA);
System.out.println(beanB);
}
 
}
 
Got Exception:- Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'b' defined in file [/home/ws/test/bin/com/circularBean/problem/B.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.circularBean.problem.A]: : Error creating bean with name 'a' defined in file [/home/ws/test/bin/com/circularBean/problem/A.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.circularBean.problem.B]: : Error creating bean with name 'b': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'b': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'a' defined in file [/home/ws/test/bin/com/circularBean/problem/A.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.circularBean.problem.B]: : Error creating bean with name 'b': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'b': Requested bean is currently in creation: Is there an unresolvable circular reference?

1 Answer

0 votes
by
To resolve circular bean references follow the following steps:
 Step1: - Make default constuctor 
 Step2: - Use setter method for injecting bean.
 
For more you see the following example:
 
package com.circularBean;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class BeanA{
 
private BeanB beanB;
 
public BeanA() {
}
 
@Autowired
public void setBeanB(BeanB beanB) {
this.beanB = beanB;
}
 
@Override
public String toString() {
return beanB.getClass().getSimpleName() + " from " + this.getClass().getSimpleName();
}
 
}
 
package com.circularBean;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class BeanB {
 
private BeanA beanA;
 
public BeanB() {
}
 
@Autowired
public void setBeanA(BeanA beanA) {
this.beanA = beanA;
}
 
@Override
public String toString() {
return beanA.getClass().getSimpleName() + " from " + this.getClass().getSimpleName();
 
}
 
}
 
package com.circularBean;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class CircularBeanMain {
 
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "resources/app-context.xml" });
BeanA beanA = applicationContext.getBean(BeanA.class);
BeanB beanB = applicationContext.getBean(BeanB.class);
System.out.println(beanA);
System.out.println(beanB);
}
 
}
 
Output:
BeanB from BeanA
BeanA from BeanB

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...