SpringBoot之ApplicationRunner接口和@Order注解
2024-09-04 16:04:11
我们在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求。该接口执行时机为容器启动完成的时候。
ApplicationRunner接口
具体代码如下:
@Component
@Order(1)
public class TestImplApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("这个是测试ApplicationRunner接口1");
}
}
@Order注解
如果有多个实现类,而你需要他们按一定顺序执行的话,可以在实现类上加上@Order注解。@Order(value=整数值)。SpringBoot会按照@Order中的value值从小到大依次执行。
@Component
@Order(2)
public class TestImplApplicationRunner2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("这个是测试ApplicationRunner接口2");
}
}
在加上@Order注解后,执行顺序会根据数字从小到大执行
最新文章
- 从SQL的视角用powershell
- sql 多行合一行
- windows下php连接sqlserver2008
- php+curl上传文件
- CentOS下搭建SVN服务器
- 2016年11月10日 星期四 --出埃及记 Exodus 20:1
- Excel中连接函数CONCATENATE()
- Restrict each user to a single session in window server 2008 R2 or 2012
- C++指针和引用
- node.js操作mongoDB数据库
- PHP Cookie学习
- Python 对Twitter中指定话题的被转载Tweet数量的频谱分析
- mongo查询系统
- js 过滤敏感词 ,可将带有标点符号的敏感词过滤掉
- 关于setTimeout的面试题
- 对于zuul服务网关框架资料整理
- liblensfun 在 mingw 上编译时遇到的奇怪问题
- java中Base64的加密工具封装
- SQL Server 4
- JAVAWEB eclipse开启服务器时,filter出现找不到的问题时
热门文章
- 微信公众号 唤醒手机导航APP 一看就懂 复制即用
- Linux学习_菜鸟教程_2
- 升级添加到现有iOS Xcode项目的Flutter
- json中含有换行符'\r','\n'的处理
- 微信小程序点击图片放大
- 79.纯 CSS 创作单元素麦当劳金拱门 Logo(原文)
- axios中请求传值方式
- Webpack实战(六):如何优雅地运用样式CSS预处理
- JS ES6补充
- [组合数学][多项式][拉格朗日插值]count