@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 with @DependsOn
.The @DependsOn
annotation may be used on any class directly or indirectly annotated with @Component
or on methods annotated with @Bean
.
Spring @DependsOn Annotation Example
Let's create an example to demonstrate using the @DependsOn
annotation in a Spring application.
1. Create a Simple Maven Project
Create a simple Maven project using your favourite IDE. Below is the project structure for your reference:
2. The pom.xml File
Make sure to use Java 17 or later for Spring Framework 6:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.8</version>
</dependency>
3. Create Spring Beans
FirstBean.java
package 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 Constructor");
}
public void populateBeans() {
secondBean.display();
thirdBean.display();
}
}
SecondBean.java
package net.javaguides.spring.dependson;
public class SecondBean {
public SecondBean() {
System.out.println("SecondBean Initialized via Constructor");
}
public void display() {
System.out.println("SecondBean method called");
}
}
ThirdBean.java
package net.javaguides.spring.dependson;
public class ThirdBean {
public ThirdBean() {
System.out.println("ThirdBean Initialized via Constructor");
}
public void display() {
System.out.println("ThirdBean method called");
}
}
4. Java-Based Configuration - AppConfig.java
Declare the above beans in a Java-based configuration class.
AppConfig.java
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({"secondBean", "thirdBean"})
public FirstBean firstBean() {
return new FirstBean();
}
@Bean("secondBean")
public SecondBean secondBean() {
return new SecondBean();
}
@Bean("thirdBean")
public ThirdBean thirdBean() {
return new ThirdBean();
}
}
5. Running Spring Application - Application.java
Create a main class to run the application.
Application.java
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();
}
}
Output
SecondBean Initialized via Constructor
ThirdBean Initialized via Constructor
FirstBean Initialized via Constructor
SecondBean method called
ThirdBean method called
As you can see in the above output, the beans SecondBean
and ThirdBean
are initialized before the bean FirstBean
. If you remove the @DependsOn
annotation from the firstBean()
method of AppConfig
class, the order of initialization of beans may be different on each run.
Conclusion
The @DependsOn
annotation in Spring ensures that certain beans are initialized before others. This is particularly useful when there are dependencies that need to be guaranteed to be up and running before others. By using this annotation, you can control the initialization order and ensure proper configuration of your Spring beans.
For more Spring and Spring Boot tutorials, you can explore related annotations like @Bean
, @Qualifier
, @Autowired
, and more. Happy coding!
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
Comments
Post a Comment
Leave Comment