SpringBoot之Order注解启动顺序

  1. order的值越小,优先级越高
  2. order如果不标注数字,默认最低优先级,因为其默认值是int最大值
  3. 该注解等同于实现Ordered接口getOrder方法,并返回数字。

 
 
  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
  3. @Documented
  4. public @interface Order {
  5. /**
  6. * The order value.
  7. * <p>Default is {@link Ordered#LOWEST_PRECEDENCE}.
  8. * @see Ordered#getOrder()
  9. */
  10. int value() default Ordered.LOWEST_PRECEDENCE;
  11. }

 

	int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
 
 

 
 
  1. @Aspect
  2. @Component
  3. public class DataSourceAspect implements Ordered {
  4. @Override
  5. public int getOrder() {
  6. return 1;
  7. }
  8. }

见下: 

OrderRunner1.java


 
 
  1. @Component
  2. @Order( 1)
  3. public class OrderRunner1 implements CommandLineRunner {
  4. @Override
  5. public void run(String... args) throws Exception {
  6. System.out.println( "The OrderRunner1 start to initialize ...");
  7. }
  8. }

 OrderRunner2.java


 
 
  1. @Component
  2. @Order( 2)
  3. public class OrderRunner2 implements CommandLineRunner {
  4. @Override
  5. public void run(String... args) throws Exception {
  6. System.out.println( "The OrderRunner2 start to initialize ...");
  7. }
  8. }

 Runner.java


 
 
  1. @Component
  2. public class Runner implements CommandLineRunner {
  3. @Override
  4. public void run(String... args) throws Exception {
  5. System.out.println( "The Runner start to initialize ...");
  6. }
  7. }

 
 
  1. @SpringBootApplication
  2. public class CommandLineRunnerApplication {
  3. public static void main(String[] args) {
  4. System.out.println( "The service to start.");
  5. SpringApplication.run(CommandLineRunnerApplication.class, args);
  6. System.out.println( "The service has started.");
  7. }
  8. }

 

它们的启动日志:


 
 
  1. The service to start.
  2. ...
  3. ...
  4. The OrderRunner1 start to initialize ...
  5. The OrderRunner2 start to initialize ...
  6. The Runner start to initialize ...
  7. The service has started.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值