You are currently viewing Using @DependsOn Annotation in Spring Boot for Bean Dependencies

Using @DependsOn Annotation in Spring Boot for Bean Dependencies

The @DependsOn annotation is used to express explicit dependencies between Spring beans. When you use @DependsOn, Spring ensures that the specified beans are initialized before the bean with the @DependsOn annotation.

Prerequisites

Make sure you have the following installed:

  • Java JDK (8 or later)
  • Maven (for building the project)
  • Your favorite Java IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Create a Spring Boot Project

First, let’s create a new Spring Boot project. You can do this easily using Spring Initializr (https://start.spring.io/), or you can use your IDE to generate a new Spring Boot project.

For this example, we’ll create a simple Spring Boot application with two beans, where one bean depends on the other.

Step 2: Create the Main Application Class

Create a main application class. This class will be the entry point of our Spring Boot application. Here’s an example:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDependsOnDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDependsOnDemoApplication.class, args);
    }
}

Step 3: Create Beans

Now let’s create two beans: BeanA and BeanB. BeanB will depend on BeanA, and we’ll use @DependsOn to ensure the dependency is resolved correctly.

BeanA.java

import org.springframework.stereotype.Component;

@Component
public class BeanA {
    public BeanA() {
        System.out.println("BeanA Initialized");
    }

    public void doSomething() {
        System.out.println("BeanA doing something");
    }
}

BeanB.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@DependsOn("beanA")
public class BeanB {
    private final BeanA beanA;

    @Autowired
    public BeanB(BeanA beanA) {
        this.beanA = beanA;
        System.out.println("BeanB Initialized");
    }

    public void doSomething() {
        System.out.println("BeanB doing something, relying on BeanA");
        beanA.doSomething();
    }
}

Step 4: Create a Service to Use the Beans

Let’s create a simple service that uses both BeanA and BeanB.

MyService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private final BeanA beanA;
    private final BeanB beanB;

    @Autowired
    public MyService(BeanA beanA, BeanB beanB) {
        this.beanA = beanA;
        this.beanB = beanB;
    }

    public void run() {
        System.out.println("MyService running");
        beanA.doSomething();
        beanB.doSomething();
    }
}

Step 5: Use the Service in Main Application

Now let’s modify the main application to use our MyService.

SpringDependsOnDemoApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringDependsOnDemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringDependsOnDemoApplication.class, args);

        MyService myService = context.getBean(MyService.class);
        myService.run();

        context.close();
    }
}

Step 6: Run the Application

Now, you can run the Spring Boot application. When you run it, you should see the following output:

BeanA Initialized
BeanB Initialized
MyService running
BeanA doing something
BeanB doing something, relying on BeanA

Explanation

  • BeanA is a simple bean that does something.
  • BeanB has a dependency on BeanA, specified with @DependsOn("beanA"). This ensures that BeanA is initialized before BeanB.
  • MyService uses both BeanA and BeanB. When MyService runs, it calls methods on both BeanA and BeanB, demonstrating the dependency between them.

Conclusion

In this tutorial, you’ve learned how to use the @DependsOn annotation in a Spring Boot application to manage bean dependencies explicitly. This can be useful when you have beans that rely on each other’s initialization order. Always remember to carefully manage dependencies to avoid circular dependencies and ensure proper bean initialization in your Spring applications.

Leave a Reply