Home  >  Spring

Spring @DependsOn Annotation Example

By Arvind Rai, November 15, 2021
This page will walk through Spring @DependsOn annotation example.
1. The @DependsOn annotation specifies the beans on which the current bean depends.
2. All beans specified by the @DependsOn annotation are guaranteed to be created by container before this bean.
3. The @DependsOn is used by a bean to avoid the side effects of another bean initialization.
4. Using @DependsOn we control initialization-time dependency. For singleton beans, the destruction-time dependency is also controlled. The bean which uses @DependsOn is destroyed first, prior to the specified beans. In this way, shutdown order can also be controlled by @DependsOn annotation.
5. The @DependsOn annotation is used with @Bean, @Component and @Service annotations.
6. The @DependsOn annotation is equivalent to <bean> element's depends-on attribute in XML configuration.
7. The @DependsOn annotation is introduced in Spring 3.0.



Using @DependsOn with @Bean

Find the code to use @DependsOn with @Bean annotation.
AppConfig.java
@Configuration
public class AppConfig {
  @Bean(name = "city")
  public City getCity() {
	return new City("Varanasi");
  }

  @Bean(name = "company")
  public Company getCompany() {
	return new Company("ABC LTD");
  }

  @DependsOn("city")
  @Bean(name = "college")
  public College getCollege() {
	return new College("SSIC");
  }

  @DependsOn("college")
  @Bean(name = "student")
  public Student getStudent() {
	return new Student(getCollege());
  }

  @DependsOn({ "city", "company" })
  @Bean(name = "employee")
  public Employee getEmployee() {
	return new Employee(getCity(), getCompany());
  }
} 
City.java
public class City {
  String name;
  public City(String name) {
	System.out.println("-- City Initialized --");	
	this.name = name;
  }
} 
Company.java
public class Company {
  String name;
  public Company(String name) {
	System.out.println("-- Company Initialized --");
	this.name = name;
  }
} 
College.java
public class College {
  String name;
  public College(String name) {
	System.out.println("-- College Initialized --");
	this.name = name;
  }
} 
Student.java
public class Student {
  College college;
  public Student(College college) {
	System.out.println("-- Student Initialized --");
  }
} 
Employee.java
public class Employee {
  City city;
  Company company;
  public Employee(City city, Company company) {
	System.out.println("-- Employee Initialized --");
	this.city = city;
	this.company = company;
  }
} 
SpringDemo.java
public class SpringDemo {
  public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(AppConfig.class);
	ctx.refresh();
  }
} 
Output
15:14:15.926 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'city'
-- City Initialized --
15:14:15.951 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'company'
-- Company Initialized --
15:14:15.952 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'college'
-- College Initialized --
15:14:15.953 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'student'
-- Student Initialized --
15:14:15.954 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'employee'
-- Employee Initialized -- 
We can observe the output that the beans are being initialized in the specified depends-on order.


Using @DependsOn with @Component

Find the code to use @DependsOn with @Component annotation.
Student.java
@DependsOn("college")
@Component
public class Student {
  College college;
  public Student(College college) {
	System.out.println("-- Student Initialized --");
  }
} 
Employee.java
@DependsOn({ "city", "company" })
@Component
public class Employee {
  City city;
  Company company;
  public Employee(City city, Company company) {
	System.out.println("-- Employee Initialized --");
	this.city = city;
	this.company = company;
  }
} 
AppConfig.java
@ComponentScan("com.concretepage")
@Configuration
public class AppConfig {
  @Bean(name = "city")
  public City getCity() {
	return new City("Varanasi");
  }

  @Bean(name = "company")
  public Company getCompany() {
	return new Company("ABC LTD");
  }

  @DependsOn("city")
  @Bean(name = "college")
  public College getCollege() {
	return new College("SSIC");
  }
} 

Circular Dependency

In circular dependency, Spring throws BeanCreationException. Find the code with circular dependency.
AppConfig.java
@Configuration
public class AppConfig {
  @DependsOn("college")
  @Bean(name = "city")
  public City getCity() {
	return new City("Varanasi");
  }
  @DependsOn("city")
  @Bean(name = "college")
  public College getCollege() {
	return new College("SSIC");
  }
  ------
} 
Our code will throw following error message.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'college' defined in com.concretepage.AppConfig: 
Circular depends-on relationship between 'college' and 'city' 

Reference

Annotation Type DependsOn

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI











©2021 concretepage.com | Privacy Policy | Contact Us