原文: http://zetcode.com/springboot/order/

Spring Boot @Order教程展示了如何使用@Order注解来规定 bean。

Spring 是流行的 Java 应用框架,而 Spring Boot 是 Spring 的演进,可以帮助轻松地创建独立的,生产级的基于 Spring 的应用。

@Order

@Order定义带注解的组件的排序顺序。 value()是可选的,表示订单值。 较低的值具有较高的优先级。

Spring Boot @Order示例

以下应用命令执行CommandLineRunner的 bean 的执行。

  1. pom.xml
  2. src
  3. ├───main
  4. ├───java
  5. └───com
  6. └───zetcode
  7. Application.java
  8. MyRunner.java
  9. MyRunner2.java
  10. └───resources
  11. └───test
  12. └───java

这是项目结构。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>springbootorder</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>11</maven.compiler.source>
  14. <maven.compiler.target>11</maven.compiler.target>
  15. </properties>
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>2.1.1.RELEASE</version>
  20. </parent>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-maven-plugin</artifactId>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. </project>

这是 Maven pom.xml文件。

com/zetcode/MyRunner.java

  1. package com.zetcode;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.core.annotation.Order;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. @Order(value = 2)
  9. public class MyRunner implements CommandLineRunner {
  10. private static final Logger logger = LoggerFactory.getLogger(MyRunner.class);
  11. @Override
  12. public void run(String... args) throws Exception {
  13. logger.info("Running MyRunner");
  14. }
  15. }

在应用启动时启动 Bean。 使用@Order注解,我们为其赋予了优先级。

com/zetcode/MyRunner2.java

  1. package com.zetcode;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.core.annotation.Order;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. @Order(value = 1)
  9. public class MyRunner2 implements CommandLineRunner {
  10. private static final Logger logger = LoggerFactory.getLogger(MyRunner.class);
  11. @Override
  12. public void run(String... args) throws Exception {
  13. logger.info("Running MyRunner2");
  14. }
  15. }

这是MyRunner2。 通过@Order设置了更高的优先级,因此在MyRunner之前执行。

com/zetcode/Application.java

  1. package com.zetcode;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

Application是设置 Spring Boot 应用的入口。

我们使用mvn -q spring-boot:run运行该应用。

在本教程中,我们展示了如何使用@Order接口来设置 bean 的执行顺序。 您可能也对相关教程感兴趣: Spring Boot @Repository教程Spring Boot CommandLineRunner教程Java 教程或列出所有 Spring Boot 教程