In this article, we will discuss how to use @DependsOn annotation in Spring Applications with an example.
The @DependsOn annotation can force the Spring IoC container to initialize one or more beans before the bean which is annotated by @DependsOn annotation.
The @DependsOn annotation may be used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean.
The following example shows how to use @DependsOn annotation in a spring application.
Spring @DependsOn annotation example
Let's create an example to demonstrate using @DependsOn annotation in a spring application.
1. Create a Simple Maven Project
Create a simple maven project using your favorite IDE and refer to the below section for packaging structure. If you are new to maven, read the article How to Create a Simple Maven Project.
2. Project Structure
The below diagram shows a project structure for your reference -
3. The pom.xml File
Note that Spring framework 6 required Java 17 or later version:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>learn-spring-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.4</version>
</dependency>
</dependencies>
</project>
4. Create Spring Beans - FirstBean, SecondBean, and ThirdBean
FirstBean.javapackage net.javaguides.spring.dependson;
import org.springframework.beans.factory.annotation.Autowired;
public class FirstBean {
@Autowired
private SecondBean secondBean;
@Autowired
private ThirdBean thirdBean;
public FirstBean() {
System.out.println("FirstBean Initialized via Constuctor");
}
public void populateBeans() {
secondBean.display();
thirdBean.display();
}
}
package net.javaguides.spring.dependson;
public class SecondBean {
public SecondBean() {
System.out.println("SecondBean Initialized via Constuctor");
}
public void display() {
System.out.println("SecondBean method called");
}
}
package net.javaguides.spring.dependson;
public class ThirdBean {
public ThirdBean() {
System.out.println("ThirdBean Initialized via Constuctor");
}
public void display() {
System.out.println("ThirdBean method called");
}
}
5. Java-Based Configuration - AppConfig.java
Declare the above beans in java based configuration class.AppConfig.java
Output:
package net.javaguides.spring.dependson;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@Configuration
public class AppConfig {
@Bean("firstBean")
@DependsOn(value = {
"secondBean",
"thirdBean"
})
public FirstBean firstBean() {
return new FirstBean();
}
@Bean("secondBean")
public SecondBean secondBean() {
return new SecondBean();
}
@Bean("thirdBean")
public ThirdBean thirdBean() {
return new ThirdBean();
}
}
6. Running Spring Application - Application.java
Let's create a main class and run an application.package net.javaguides.spring.dependson;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
FirstBean bean = context.getBean(FirstBean.class);
bean.populateBeans();
context.close();
}
}
SecondBean Initialized via Constuctor
ThirdBean Initialized via Constuctor
FirstBean Initialized via Constuctor
SecondBean method called
ThirdBean method called
As you can see in the above output, the beans SecondBean and ThirdBean are initialized before bean FirstBean.
If you remove the @DependsOn annotation from the firstBean() method of AppConfig class, the output (i.e. order of initialization of beans) of the main class will be different on each run like:
FirstBean Initialized via Constuctor
SecondBean Initialized via Constuctor
ThirdBean Initialized via Constuctor
SecondBean method called
ThirdBean method called
The source code of this article is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course