Circular Dependency in Spring | Spring Circular Dependency proble

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about what is Spring circular dependency problem? and how to resolve it.
In our day to day development life, while implementing the spring applications, we may have a requirement like class A depending on class B and class B also depending on class A,  though it is not a good practice sometimes you can’t avoid such types of dependencies.
Spring considers this type of scenario as a circular dependency and is also given some good practices to handle this. Here, I am going to show you the most popular way to handle this scenario.
In spring we have different types of dependency injections if we inject the dependency through the constructor arguments <constructor-arg>, and the two classes are depending on each other then spring will throw an exception like BeanCurrentlyInCreationException, this is where we get circular dependency in spring.
In the case of constructor injection, the dependencies are injected while creating the object, so that, by the time creating the object of A, there is no object of B and vice versa.
class A {
    private B b;
    public A(B b) {
        this.b = b;
    }
}

class B {
    private A a;
    public B(A a) {
        this.a = a;
    }
}
On the above, while creating object A, object B is required. Similarly to create an object for B, object A is required. Finally, A and B objects will not be created as these two are depends on each other and an exception will be thrown by the spring container.
In order to solve the circular dependency problem on one side, the dependency injection type must be changed to setter injection. In the case of setter injection, the dependencies are injected after creating the object, not while creating the objects.
class A {
    private B b;
    public void setB(B b) {
        this.b = b;
    }
}

class B {
    private A a;
    public void setA(A a) {
        this.a = a;
    }
}
In the above, the Spring container will inject objects of class B to A, like the following.
A a = new A();
B b = new B();
a.setB(b);
Spring ICO container
Spring Loaded beans in IOC container
Happy Learning 🙂

Dependency Injection (IoC) in spring with Example
Spring Collection Dependency List Example
Spring Collection Map Dependency Example
@Autowired,@Qualifier,@Value annotations in Spring
Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByName Example
Spring Bean Autowire ByType Example
BeanFactory Example in Spring
spring expression language example
Advantages of Spring Framework
@Component,@Service,@Repository,@Controller in spring
Types of Spring Bean Scopes Example
Spring Java Configuration Example
Spring JdbcTemplate CRUD Application
@Qualifier annotation example in Spring

Dependency Injection (IoC) in spring with Example
Spring Collection Dependency List Example
Spring Collection Map Dependency Example
@Autowired,@Qualifier,@Value annotations in Spring
Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByName Example
Spring Bean Autowire ByType Example
BeanFactory Example in Spring
spring expression language example
Advantages of Spring Framework
@Component,@Service,@Repository,@Controller in spring
Types of Spring Bean Scopes Example
Spring Java Configuration Example
Spring JdbcTemplate CRUD Application
@Qualifier annotation example in Spring