@Order注解不能指定 bean 的加载顺序,它适用于 AOP 的优先级,以及将多个 Bean 注入到集合时,这些 bean 在集合中的顺序。
注意:组件类必须实现接口
package com.wenxiaowu.springboot.order.component; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * 测试组件order加载顺序(未实现接口,不能正确加载顺序) */ @SpringBootApplication(scanBasePackages = {"com.wenxiaowu.springboot.order.component"}) public class ComponentOrderTest { public static void main(String[] args) { System.out.println("ComponentOrderTest start."); SpringApplication.run(ComponentOrderTest.class, args); System.out.println("ComponentOrderTest end."); } } @Component class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize ..."); } } @Component @Order(2) class OrderRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The OrderRunner1 start to initialize ..."); } } @Component @Order(1) class OrderRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The OrderRunner2 start to initialize ..."); } }
package com.wenxiaowu.springboot.order.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.List; /** * 测试组件order加载顺序 */ @SpringBootApplication(scanBasePackages = {"com.wenxiaowu.springboot.order.service"}) public class ServiceOrderTest { public static void main(String[] args) { System.out.println("ServiceOrderTest start."); SpringApplication.run(ServiceOrderTest.class, args); System.out.println("ServiceOrderTest end."); } } @Component class AfterRepairConsumer { @Autowired private List<RepairCreatePostConsumer> postConsumers; @PostConstruct public void run() { final String repairId = "1"; if (postConsumers != null && postConsumers.size() > 0) { postConsumers.forEach(e -> e.postHandler(repairId)); } } } interface RepairCreatePostConsumer { /** * 创建报修单后做什么 */ void postHandler(String repairId); } @Service @Order(value = 3) class SendEmail implements RepairCreatePostConsumer { @Override public void postHandler(String repairId) { System.out.println("为报修单" + repairId + "发送邮件"); } } @Service @Order(value = 2) class SendInvoice implements RepairCreatePostConsumer { @Override public void postHandler(String repairId) { System.out.println("为报修单" + repairId + "发送发票"); } } @Service @Order(value = 1) class SendMessage implements RepairCreatePostConsumer { @Override public void postHandler(String repairId) { System.out.println("为报修单" + repairId + "发送消息"); } }