"> " />

Prev Next

Spring / Spring Beans

Explain Circular Dependency scenario in Spring.

Consider a scenario where Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection. If you configure beans for the classes A and B to be injected into each other, the Spring IoC container detects this circular reference at runtime and throws a BeanCurrentlyInCreationException.

The easiest solution to resolve the circular dependency issues would be editing the classes to have the dependency set by setters rather than constructor.

public class A {

    public A() {
        System.out.println("Creating instance of A");

    private B b;

    public void setB(B b) {
        System.out.println("Setting dependency B of A instance");
        this.b = b;

public class B {

    public B() {
        System.out.println("Creating instance of B");

    private A a;

    public void setA(A a) {
        System.out.println("Setting dependency A of B instance");
        this.a = a;

<bean id="a" class="mypackage.A">
    <property name="b" ref="b" />

<bean id="b" class="mypackage.B">
    <property name="a" ref="a" />

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

What are Beans in Spring framework? What does a Spring Bean definition consists of? Different Spring Bean Scope. How do I provide beans configuration metadata to the Spring Container? How do I define the scope of a bean in spring framework? Describe the lifecycle of a Bean in Spring framework. Is a singleton bean thread safe in Spring Framework? Explain the important life-cycle methods of a bean in Spring framework. Explain inner beans in Spring framework? What are the limitations of autowiring in Spring framework? How do I inject java.util.Properties into a Spring Bean? Explain @Required annotation in Spring framework. Explain @autowired annotation in spring framework. How do I make autowired bean property as optional? What is @Resource annotation in Spring framework? How do I configure a bean in my spring application? What kind of exception does spring DAO classes throw? Explain the different ways to configure a Java class as Spring Bean. Difference between id and name attribute of <bean> element in a Spring configuration file. Is Spring Bean with singleton scope thread safe? Difference between @Resource and @Autowired in Spring framework. Explain lazy-init attribute of bean tag in Spring. Can we fetch the inner bean from spring container using its id, if an inner bean defines one? Explain Spring bean inheritance. What is Bean Definition Template in Spring framework? What is spring bean pure inheritance template? Can a spring bean tag with no id or name attribute be created? How do I inject value for a bean property of primitive type? Can spring bean autowire on primitive types? How do I create a stateful bean in Spring framework? Difference between request and prototype bean scope in spring? What is the role of a Spring bean configuration file? Explain Circular Dependency scenario in Spring. Spring Bean marked as abstract by abstract=true needs the corresponding java class be abstract ? Spring Bean: Specify particular implementation of collection in your bean definition. Spring bean: difference between stateless and stateful beans. Can I define spring bean twice with same name in different bean definition file? Can I define spring bean twice with the same bean id in same file? Explain JSR-250 annotation. What is primary annotation in Spring? How to get all the Spring beans implementing a specific interface? What are the attributes of Spring @Transactional annotation? When is a spring bean destroy-method being called? When is a Spring Singleton bean garbage collected? When is a prototype bean garbage collected? Explain BeanPostProcessor beans. Explain no autowiring mode in spring bean. How do you debug Spring configuration? What is component scan in Spring framework?
Show more question and Answers...

Spring Interview questions II

Comments & Discussions