Springboot学习—CommandLineRunner接口(转载)
2024-09-04 21:03:16
前言
Spring boot的CommandLineRunner
接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
如何使用CommandLineRunner接口
我们可以用以下三种方式去使用CommandLineRunner
接口:
1)和@Component注解一起使用
这种使用方式相当简便,如下所示:
@Component
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunner run method Started !!");
}
}
2)和@SpringBootApplication注解一起使用
这种使用方式也相当的简单,参考代码如下:
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
} public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
} @Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}
3)声明一个实现了CommandLineRunner接口的Bean
这种方式其实也大同小异,就是在SpringBootApplication
里定义一个Bean,改Bean实现了CommandLineRunner
接口,参考代码如下:
ApplicationStartupRunner.java
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}
注册ApplicationStartupRunner bean
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
} public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
} @Bean
public ApplicationStartupRunner schedulerRunner() {
return new ApplicationStartupRunner();
}
}
注意:在实现
CommandLineRunner
接口时,run(String… args)
方法内部如果抛异常的话,会直接导致应用启动失败,所以,一定要记得将危险的代码放在try-catch
代码块里。
用@Order注解去设置多个CommandLineRunner实现类的执行顺序
一个应用可能存在多个CommandLineRunner
接口实现类,如果我们想设置它们的执行顺序,可以使用 @Order
实现
@Order(value=3)
@Component
class ApplicationStartupRunnerOne implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunnerOne run method Started !!");
}
} @Order(value=2)
@Component
class ApplicationStartupRunnerTwo implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunnerTwo run method Started !!");
}
}
输出日志:
2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !!
2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !!
为什么要使用CommandLineRunner接口
- 实现在应用启动后,去执行相关代码逻辑,且只会执行一次;
- spring batch批量处理框架依赖这些执行器去触发执行任务;
我们可以在run()方法里使用任何依赖,因为它们已经初始化好了;
转载自:
Spring boot CommandLineRunner接口使用例子
最新文章
- 我为NET狂官方面试题
- Spring(1)
- Newtonsoft.Json 的序列化与反序列化
- 20145224&;20145238《信息安全系统设计基础》实验五
- [转载]QQ空间技术架构之深刻揭密
- (实用篇)PHP缓存类完整实例
- OpenJudge计算概论-与7无关的数
- Oracle 表数据去重
- karma note
- poj3764(dfs+Trie树+贪心)
- Educational Codeforces Round 15_C. Cellular Network
- wemall app商城系统Android之支付宝接口RSA函数
- DUMP4 企业级电商项目 —— 对接支付宝扫码支付
- 第四次java实验
- mongoDB创建windows服务启动解决
- 史上最全 原生javascript的知识总结,适合新手及查资料用!
- linux下自定义dubbo的shell脚本
- IMU 预积分推导
- 剖析管理所有大数据组件的可视化利器:Hue
- 关于clearfix和clear的研究
热门文章
- HDU - 5557 Matching Compressed String (自动机+倍增+表达式计算)
- LINUX查看内存使用情况 free
- glRenderbufferStorageMultisample
- Excel 中大量图片如何快速导出? 转载自:http://www.zhihu.com/question/20800948
- struts2模糊查询
- HDU 6049 - Sdjpx Is Happy | 2017 Multi-University Training Contest 2
- CSP-S 模拟测试 51 题解
- 【CUDA 基础】4.5 使用统一内存的向量加法
- JavaWeb_(Spring框架)在Struts+Hibernate框架中引入Spring框架
- Java写入的常用技巧(二)