In this article, we will discuss how to use the @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
.
Let's create an example to demonstrate using the @DependsOn
annotation in a Spring application.
Let's create an example to demonstrate using the @DependsOn
annotation in a Spring application.
Make sure to use Java 17 or later for Spring Framework 6:
<dependency>
<groupId>org.springframework
<artifactId>spring-core
<version>6.1.8
</dependency>
<dependency>
<groupId>org.springframework
<artifactId>spring-context
<version>6.1.8
</dependency>
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();
}
}
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");
}
}
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"); } }
Declare the above beans in a Java-based configuration class.
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();
}
}
Create a main class to run the 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 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.
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!