Spring @DependsOn Annotation Example

In this section we will learn about @DependsOn Annotation.


The order in which Spring container creating beans cannot be predicted. When we need spring to create certain bean before creating our bean, we can choose the @DependsOn annotation for managing bean creation order.  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 annotated with @Component, @Service & @Repository or on methods annotated with @Bean.

1. Using @DependsOn on @Bean method

@DependsOn annotation accepts array of bean names to specify the dependency beans. And again, here the order we keep the bean name in array is the order for bean creation. For the below code, the order of bean creation is twitterService, emailService, smsService.

@Configuration
public class AppConfig {

@Bean
@DependsOn({"twitterService", "emailService"})
public SMSService smsService()
{
return new SMSService();
}

@Bean
public TwitterService twitterService()
{
return new TwitterService();
}

@Bean
public EmailService emailService()
{
return new EmailService();
}
}

public class SMSService {

public SMSService() {
System.out.println("Inside SMSService Constuctor");
}
}

public class EmailService {

public EmailService() {
System.out.println("Inside EmailService Constuctor");
}
}

public class TwitterService {

public TwitterService() {
System.out.println("Inside TwitterService Constuctor");
}
}


2. Using @DependsOn on @Component class

For the below code, the order of bean creation is adminService, userService, employeeService.

@Component
@DependsOn({"adminService", "userService"})
public class EmployeeService {

public EmployeeService() {
System.out.println("Inside EmployeeService Constuctor");
}
}

@Component
public class AdminService {

public AdminService() {
System.out.println("Inside AdminService Constuctor");
}
}

@Component
public class UserService {

public UserService() {
System.out.println("Inside UserService Constuctor");
}
}


The following example creates a Spring Boot application which uses @DependsOn annotation.

Project Directory



pom.xml

<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-dependson-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-dependson-annotation-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


EmailService.java

package com.knf.dev.demo.service;

public class EmailService {

public EmailService() {
System.out.println("Inside EmailService Constuctor");
}
}


SMSService.java

package com.knf.dev.demo.service;

public class SMSService {

public SMSService() {
System.out.println("Inside SMSService Constuctor");
}
}


TwitterService.java

package com.knf.dev.demo.service;

public class TwitterService {

public TwitterService() {
System.out.println("Inside TwitterService Constuctor");
}
}


AppConfig.java

package com.knf.dev.demo.config;

import com.knf.dev.demo.service.EmailService;
import com.knf.dev.demo.service.SMSService;
import com.knf.dev.demo.service.TwitterService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
public class AppConfig {

@Bean
@DependsOn({"twitterService", "emailService"})
public SMSService smsService()
{
return new SMSService();
}

@Bean
public TwitterService twitterService()
{
return new TwitterService();
}

@Bean
public EmailService emailService()
{
return new EmailService();
}
}


AdminService.java

package com.knf.dev.demo.component;

import org.springframework.stereotype.Component;

@Component
public class AdminService {

public AdminService() {
System.out.println("Inside AdminService Constuctor");
}
}


UserService.java

package com.knf.dev.demo.component;

import org.springframework.stereotype.Component;

@Component
public class UserService {

public UserService() {
System.out.println("Inside UserService Constuctor");
}
}


EmployeeService.java

package com.knf.dev.demo.component;

import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

@Component
@DependsOn({"adminService", "userService"})
public class EmployeeService {

public EmployeeService() {
System.out.println("Inside EmployeeService Constuctor");
}
}


Run the application - Application.java

package com.knf.dev.demo;

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

@SpringBootApplication
public class Application {


public static void main(String[] args) {

ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args);

context.close();
}
}

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning. 

Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run

Console Output:
Inside AdminService Constuctor
Inside UserService Constuctor
Inside EmployeeService Constuctor
Inside TwitterService Constuctor
Inside EmailService Constuctor
Inside SMSService Constuctor


Download Source Code

More related topics,

Spring Web Annotations


Spring Core Annotations

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Top 5 Java ORM tools - 2024

Java - How to Count the Number of Occurrences of Substring in a String