SpringBoot之Order注解启动顺序
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
版权声明:本文为博主原创文章,转载请附上博文链接!
最新文章
- 如何用C#+WinRAR 实现压缩 分类:
- Java:基于LinkedList实现栈和队列
- <;记录学习>;京东页面最后一天HTML以及css遇到的问题
- JavaWeb学习总结-07 Filter 学习和使用
- nohup启动java命令导致dubbo无法注册
- jQuery 选择器语法
- [Effective JavaScript 笔记]第30条:理解prototype、getPrototypeOf和__ptoto__之间的不同
- Strassen算法
- 实现带有getMin的栈
- LeetCode_Swap Nodes in Pairs
- SQL Server 2008 忘记sa密码的解决办法
- C#用正则表达式 获取网页源代码标签的属性或值
- java基础(九章)
- 深入浅出AQS之条件队列
- Spring Boot 2.0 教程 | 配置 Undertow 容器
- Java中使用MongoDB
- Html5游戏开发-图形与动画(一)
- windows版 Java调用人脸识别离线sdk
- You Don&#39;t Know JS: this &; Object Prototypes( 第5章 Prototypes)
- list的四种遍历方式