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

Carvia Tech | April 27, 2019 | 2 min read | 236 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
    @Component("bean1")
    public class Bean1 {

        @Autowired
        private Bean2 bean2;   (1)
    }


    @Component("bean1")
    public class Bean2 {

        @Autowired
        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
    @Component("bean1")
    public class Bean1 {

        @Autowired
        private Bean2 bean2;

        @PostConstruct          (1)
        public void init() {
            bean2.setBean1(this);
        }
    }


    @Component("bean2")
    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. Spring Boot 2.0 Reactive Web Performance Metrics
  3. How will you test web layer in Spring Boot using WebMvcTest annotation?
  4. Prevent Lost Updates in Database Transaction using Spring Hibernate
  5. How to prevent duplicate form submission in Spring MVC
  6. How to create a custom banner for your spring boot application
  7. File Upload in Spring MVC using RestTemplate with ByteArrayResource & FileSystemResource



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 month ago


Recommended books for interview preparation:

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