SpringBoot之CommandLineRunner接口和ApplicationRunner接口区别
2016-12-27 00:00
836 查看
我们在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为CommandLineRunner和ApplicationRunner。他们的执行时机为容器启动完成的时候。
这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。下面我写两个简单的例子,来看一下这两个接口的实现。
这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。下面我写两个简单的例子,来看一下这两个接口的实现。
CommandLineRunner
具体代码如下:import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * Created by zkn on 2016/8/12. */ @Component public class TestImplCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("<<<<<<<<<<<<这个是测试CommandLineRunn接口>>>>>>>>>>>>>>"); } }
ApplicationRunner接口
具体代码如下:import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; /** * Created by zkn on 2016/8/12. * 注意:一定要有@Component这个注解。要不然SpringBoot扫描不到这个类,是不会执行。 */ @Component public class TestImplApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(args); System.out.println("这个是测试ApplicationRunner接口"); } }
@Order注解
如果有多个实现类,而你需要他们按一定顺序执行的话,可以在实现类上加上@Order注解。@Order(value=整数值)。SpringBoot会按照@Order中的value值从小到大依次执行。Tips
如果你发现你的实现类没有按照你的需求执行,请看一下实现类上是否添加了Spring管理的注解(@Component)。相关文章推荐
- apk增量升级
- Android DB
- Android AES加解密
- Android 系统提供的文件下载
- android 文件监听器
- 阿里技术协会好文推荐:Android绘制流程
- ios App启动加载广告页面思路
- android 学习笔记1-介绍 环境 简单布局
- 收集移动端HTML5/H5使用的插件
- Swift3.0 运行时获取对象属性列表
- iOS开发 - 在状态栏显示FPS,CPU和内存信息
- 关于Android Studio导入源码的问题解决
- iOS pch文件的作用和用法(附:在release版本禁止输出NSLog内容)
- 跳过 Xcode 下载 iOS 模拟器
- virtualenvwrapper详解
- android4.4以上设置状态栏透明
- 微信小程序探究
- unity分辨率相关
- Android 环境搭建资料及启动过程中问题汇总
- Android 动态 添加 View