Spring @DependsOn Annotation Example

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.

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:

Spring @DependsOn Annotation Example

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

Comments