order的规则:

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Documented
public @interface Order {

/**
* The order value.
* <p>Default is {@link Ordered#LOWEST_PRECEDENCE}.
* @see Ordered#getOrder()
*/
int value() default Ordered.LOWEST_PRECEDENCE;

}

int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
@Aspect
@Component
public class DataSourceAspect implements Ordered {

@Override
public int getOrder() {
return 1;
}

}
见下:

OrderRunner1.java

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("The OrderRunner1 start to initialize ...");
}
}
 OrderRunner2.java

@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("The OrderRunner2 start to initialize ...");
}
}
 Runner.java

@Component
public class Runner implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("The Runner start to initialize ...");
}
}
@SpringBootApplication
public class CommandLineRunnerApplication {

public static void main(String[] args) {
System.out.println("The service to start.");
SpringApplication.run(CommandLineRunnerApplication.class, args);
System.out.println("The service has started.");
}
}

它们的启动日志:

The service to start.
...
...
The OrderRunner1 start to initialize ...
The OrderRunner2 start to initialize ...
The Runner start to initialize ...
The service has started.
 
---------------------
作者:jiangxwa
来源:CSDN
原文:https://blog.csdn.net/jiangxwa/article/details/87892577
版权声明:本文为博主原创文章,转载请附上博文链接!

SpringBoot之Order注解启动顺序的更多相关文章

  1. SpringBoot之ApplicationRunner接口和@Order注解

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...

  2. spring注解@postConstruct与constructor与@Autowired的启动顺序

    本文链接:https://blog.csdn.net/zpflwy1314/article/details/80797756 @Postcontruct’在依赖注入完成后自动调用,例如要将对象a注入到 ...

  3. 浅谈Spring @Order注解的使用(转)

    注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: 1 ...

  4. @Order注解使用

    注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: @ ...

  5. spring中Order注解

    Spring在加载Bean的时候,有用到order注解. PS:顾名思义,Order是顺序,此注解可操作于类.方法.字段,当作用在类时,值越小,则加载的优先级越高! @Retention(Retent ...

  6. SpringBoot内置tomcat启动原理

    前言          不得不说SpringBoot的开发者是在为大众程序猿谋福利,把大家都惯成了懒汉,xml不配置了,连tomcat也懒的配置了,典型的一键启动系统,那么tomcat在springb ...

  7. springboot之swagger快速启动

    springboot之swagger快速启动 简介 介绍 可能大家都有用过swagger,可以通过ui页面显示接口信息,快速和前端进行联调. 没有接触的小伙伴可以参考官网文章进行了解下demo页面. ...

  8. SpringBoot(15)—@Conditional注解

    SpringBoot(15)-@Conditional注解 作用 @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件的才给容器注册Bean. 一.概述 1 ...

  9. springboot基础、注解等

    SpringBoot 1.springboot概念 Spring Boot是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...

随机推荐

  1. JDK8中的ConcurrentHashMap源码

    背景 上文JDK8中的HashMap源码写了HashMap,这次写ConcurrentHashMap ConcurrentHashMap源码 /** * Maps the specified key ...

  2. Github版本控制系统

    Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). 特别推荐简单易懂的廖雪锋大神制作的学习教程: https://www.liaoxuefeng.com/wiki/896043 ...

  3. js求两个整数的百分比

    function GetPercent(num, total) {                          num = parseFloat(num);                   ...

  4. MyEclipse 8.5整合Git,并在Github上发布项目

    我们在闲暇时间想加入些团队做点属于自己有意义的东西,那Github就是为你准备的.但是用惯SVN的我们就得学习学习了. 工具/原料 myeclipse8.5 github 方法/步骤 1 下载Ecli ...

  5. mysql5.7.26安装

    h https://www.cnblogs.com/zgqbky/p/11699282.html 张国强(明了) tar -xf mysql-5.7.26-1.el7.x86_64.rpm-bundl ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:图片响应式 (将很好地扩展到父元素)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. CCF 201703-4 地铁修建(最小生成树)

    题意:A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁.地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通枢纽之 ...

  8. [转载]@Component 和 @Bean 的区别

    @Component 和 @Bean 的区别 @Component 和 @Bean 的区别 Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean. 完成这两个动作有三种方 ...

  9. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  10. Educational Codeforces Round 65 选做

    好久没更博客了,随便水一篇 E. Range Deleting 题意 给你一个长度为 \(n\) 的序列 \(a_1,a_2,\dots a_n\) ,定义 \(f(l,r)\) 为删除 \(l\le ...