Spring @DependsOn注解示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(4.1k)|赞(0)|评价(0)|浏览(951)

在这篇文章中,我们将通过一个例子讨论如何在Spring应用程序中使用@DependsOn注解。

@DependsOn注解可以强制Spring IoC容器在被@DependsOn注解的bean之前初始化一个或多个bean。
@DependsOn注解可以用在任何直接或间接用@Component注解的类或用@Bean注解的方法上。

下面的例子展示了如何在一个spring应用程序中使用@DependsOn注解。

Spring @DependsOn注解示例

让我们创建一个例子来演示在spring应用程序中使用@DependsOn注解的用法。

使用的工具和技术

  • Spring Framework - 5.1.0.RELEASE
  • JDK - 8或更高版本
  • Maven - 3.2以上
  • IDE - Eclipse Mars/STS

1. 创建一个简单的Maven项目

使用你喜欢的IDE创建一个简单的Maven项目,并参考下面的打包结构。如果你是maven新手,请阅读本文《如何创建一个简单的Maven项目》。

2. 项目结构

3. pom.xml文件

<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>

4. 创建Spring Bean - FirstBean, SecondBean, and ThirdBean

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");
    }
}

5. 基于Java的配置 - AppConfig.java

在基于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();
    }
}

6. 运行Spring应用程序 - 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();
    }
}

输出

SecondBean Initialized via Constuctor
ThirdBean Initialized via Constuctor
FirstBean Initialized via Constuctor
SecondBean method called
ThirdBean method called

正如你在上面的输出中看到的,豆类SecondBeanThirdBean在豆类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中找到。

相关文章