☰ See All Chapters |
Spring @DependsOn Annotation
When you need spring to create certain bean before creating your bean, you can choose either the SmartLifeCycle interface or 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. Spring creates the beans in the order in which bean methods are written in spring configuration class. For the below configuration, the order of bean creation is accountService, loginService, logoutService.
@Configuration public class SpringConfiguration {
@Bean public AccountService accountService () { System.out.println("AccountService Bean creation"); return new AccountService(); }
@Bean public LogoutService logoutService() { System.out.println("LogoutService Bean creation"); return new LogoutService(); }
@Bean public LoginService loginService() { System.out.println("LoginService Bean creation"); return new LoginService(); }
} |
But we need loginService, logoutService beans to be created before accountService. We have to change the order of methods we wrote in the class. In real time maintaining the order of methods is not practical, we can have many configuration classes and many bean methods. We use @DependsOn annotation to specify the beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean.
@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 loginService, logoutService, accountService.
@Configuration public class SpringConfiguration {
@Bean @DependsOn({"loginService", "logoutService"}) public AccountService accountService () { System.out.println("AccountService Bean creation"); return new AccountService(); }
@Bean public LogoutService logoutService() { System.out.println("LogoutService Bean creation"); return new LogoutService(); }
@Bean public LoginService loginService() { System.out.println("LoginService Bean creation"); return new LoginService(); }
} |
@DependsOn annotation example
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>Spring3_DependsOn_Example</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3_DependsOn_Example</name> <url>https://maven.apache.org</url> <properties> <org.springframework.version>3.2.0.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
LoginService.java
package com.java4coding;
public class LoginService { }
|
LogoutService.java
package com.java4coding;
public class LogoutService { } |
AccountService.java
package com.java4coding;
public class AccountService { } |
SpringConfiguration.java
package com.java4coding;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn;
import com.java4coding.LogoutService;
@Configuration public class SpringConfiguration {
@Bean @DependsOn({"loginService", "logoutService"}) public AccountService accountService () { System.out.println("AccountService Bean creation"); return new AccountService(); }
@Bean public LoginService loginService() { System.out.println("LoginService Bean creation"); return new LoginService(); }
@Bean public LogoutService logoutService() { System.out.println("LogoutService Bean creation"); return new LogoutService(); }
}
|
Demo.java
package com.java4coding;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Demo { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding" ); ((ConfigurableApplicationContext) context).close(); } }
|
Project Directory Structure
Output:
Output without using @DependsOn:
All Chapters