在这篇文章中,我们将通过一个例子讨论如何在Spring应用程序中使用@DependsOn
注解。
@DependsOn
注解可以强制Spring IoC容器在被@DependsOn
注解的bean之前初始化一个或多个bean。@DependsOn
注解可以用在任何直接或间接用@Component
注解的类或用@Bean注解的方法上。
下面的例子展示了如何在一个spring应用程序中使用@DependsOn注解。
让我们创建一个例子来演示在spring应用程序中使用@DependsOn
注解的用法。
使用你喜欢的IDE创建一个简单的Maven项目,并参考下面的打包结构。如果你是maven新手,请阅读本文《如何创建一个简单的Maven项目》。
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides.spring</groupId>
<artifactId>spring-dependson-annotation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-scope-example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
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 Constuctor");
}
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 Constuctor");
}
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 Constuctor");
}
public void display() {
System.out.println("ThirdBean method called");
}
}
在基于java的配置类中声明上述Bean。
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(value = {
"secondBean",
"thirdBean"
})
public FirstBean firstBean() {
return new FirstBean();
}
@Bean("secondBean")
public SecondBean secondBean() {
return new SecondBean();
}
@Bean("thirdBean")
public ThirdBean thirdBean() {
return new ThirdBean();
}
}
让我们创建一个主类并运行一个应用程序。
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 Constuctor
ThirdBean Initialized via Constuctor
FirstBean Initialized via Constuctor
SecondBean method called
ThirdBean method called
正如你在上面的输出中看到的,豆类SecondBean
和ThirdBean
在豆类FirstBean
之前被初始化。
如果你从AppConfig
类的firstBean()
方法中移除@DependsOn
注解,主类的输出(即豆类的初始化顺序)在每次运行时都会有所不同,如:
FirstBean Initialized via Constuctor
SecondBean Initialized via Constuctor
ThirdBean Initialized via Constuctor
SecondBean method called
ThirdBean method called
本文的源代码可在我的GitHub仓库https://github.com/RameshMF/spring-core-tutorial中找到。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/10/spring-dependson-annotation-example.html
内容来源于网络,如有侵权,请联系作者删除!