What is Circular dependency in Spring Boot?

By | May 1, 2024

What is Dependency?

When a class or object depends on another class or object for its functioning, it is called dependency. For example, Say we have two classes: company and employee, and the company class needs employees to work. We can say that company class depends on the employee.

What is Circular Dependency?

When we have two classes that depend on each other for their functioning, this is called circular dependency. Considering the above example, the company and employee classes can depend on each other. They can be considered to form a circular dependency.

What is Circular dependency in Spring Boot?
Fig 1- Circular dependency

Cyclic dependency issue in Spring Boot

In the above example of the company and employee classes Say, the Spring framework first starts initializing the company class, and it finds the dependency of the employee class. So, it goes to initialize the employee class, and there it finds the dependency of the company class, which leads to a cyclic dependency issue.

Lets code the example and understand the cyclic dependency issue in Spring Boot:

First of all, we will create a Spring Boot application, and inside it, we will create two classes: company and employee, respectively.

Java
import org.springframework.context.annotation.Configuration;

/**
 * 1. We have created an employee instance in the Company class
 * 2. Using Constructor injection, we are injecting 
 *    the dependency of employee class
 * 
 * @author paulsofts
 */

@Configuration
public class Company {

	private Employee employee;

	public Company(Employee employee) {
		this.employee = employee;
	}

}

Similarly, we will create an employee class and inject the dependency of the company class into it.

Java
import org.springframework.context.annotation.Configuration;

@Configuration
public class Employee {
	
	private Company company;
	
	public Employee(Company company) {
		this.company = company;
	}

}

As we can see above, we have added the @Configuration annotation to both classes, which means that as soon as we run our application, Spring Boot will ensure the initialization of these classes.

Let’s run and test the application.

Fig 2- Circular dependency in Spring Boot

In Spring Boot, we can use the @Autowired annotation to inject the dependencies. Even if we use the annotations to inject the dependencies, we will face the same cyclic dependency issue as the autowired annotation internally uses the constructor injection itself.

How to resolve cyclic dependency in Spring Boot?

We can resolve the cyclic dependency with the help of setter injection. In setter-injection, we use setters instead of constructors to inject the dependency. Let’s update our company and employee classes and use setter injection to inject the dependencies into each other.

Java
import org.springframework.context.annotation.Configuration;

/**
 * 1. We have created an employee instance in the Company class
 * 2. Using setter injection, we are injecting 
 *    the dependency of employee class
 * 
 * @author paulsofts
 */

@Configuration
public class Company {

	private Employee employee;

//	public Company(Employee employee) {
//		this.employee = employee;
//	}
	
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

}

Similarly, in the employee class, we will use setters to inject the dependencies.

Java
import org.springframework.context.annotation.Configuration;

@Configuration
public class Employee {
	
	private Company company;
	
//	public Employee(Company company) {
//		this.company = company;
//	}
	
	public void setCompany(Company company) {
		this.company = company;
	}

}

Again, we will run our application, and it will be up and running successfully without any errors.

Fig 3- Cyclic dependency in Spring Boot resolved

Leave a Reply

Your email address will not be published. Required fields are marked *