How will you handle unresolved circular dependency in spring dependency injection?

Carvia Tech | April 27, 2019 | 2 min read | 1,326 views

What is Circular Dependency in Spring?

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

A circular dependency between bean A and bean B forces one of the beans to be injected into the other prior to being fully initialized itself (a classic chicken/egg scenario).

Circular Dependncy - A Symptom of Bad Design

Circular dependencies are usually a symptom of bad design!

This is most likely a design level problem and should be resolved by modifying the object level design. This problem indicates that two different Classes share the common responsibility and your design is lacking the abstraction of responsibilities.

Example Code for Circular Dependency

Bean1 java source
    public class Bean1 {

        private Bean2 bean2;   (1)

    public class Bean2 {

        private Bean1 bean1;  (2)
1 Bean1 is dependent on Bean2
2 Bean2 is dependent on Bean1

Possible Solutions

  1. Refactor the design to remove circular dependency. This is the only elegant way of breaking this circular dependency, only a cleaner design with orthogonal responsibilities can solve the problem in its entirty.

  2. Other possible solution is to edit the source code of some classes to be configured by setters rather than constructors. Alternatively, avoid constructor injection and use setter injection only. In other words, although it is not recommended, you can configure circular dependencies with setter injection. This may work for few specific scenario’s but its still a workaround.

  3. Third option is to specify one way dependency through Injection using @Autowired and other way dependency via invoking setter manually from PostConstruct method of first bean.

3rd Approach - Java Source
    public class Bean1 {

        private Bean2 bean2;

        @PostConstruct          (1)
        public void init() {

    public class Bean2 {

        private Bean1 bean1;    (2)

        public void setBean1(Bean1 bean1) {
            this.bean1 = bean1;
1 Injecting the dependency on another bean using @PostConstruct
2 Do no specify @Autowired as DI is not used for Bean1 in Bean2

Top articles in this category:
  1. Spring DI - Singleton beans with prototype-bean dependencies
  2. Feign exception handling in Spring Cloud
  3. Feign RequestInterceptor in Spring Boot
  4. How to prevent duplicate form submission in Spring MVC
  5. SendGrid emails in Spring Boot
  6. Custom banner in spring boot
  7. Java AWS Lambda using Spring Cloud Function

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info