博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 5:应用程序启动时初始化资源
阅读量:4309 次
发布时间:2019-06-06

本文共 1835 字,大约阅读时间需要 6 分钟。

 

需求:应用程序启动后,初始化基础数据、加密证书等操作。

可以使用CommandLineRunner接口来实现,在SpringBoot.run()之后完成资源的初始化工作。

注意:多个Runner需要顺序启动的话,可以使用@Order注解

package sun.flower.diver.modules.system.init;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * 应用程序启动后加载基础数据 Runner * * @Author YangXuyue * @Date 2018/10/28 13:48 */@Component@Order(1) public class BaseDataRunner implements CommandLineRunner { @Override public void run(String... strings) throws Exception { System.out.println("start init base data"); } }
package sun.flower.diver.modules.system.init;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * 应用程序启动后加载证书 Runner * * @Author YangXuyue * @Date 2018/10/28 13:50 */@Component@Order(2) public class CertificateRunner implements CommandLineRunner { @Override public void run(String... strings) throws Exception { System.out.println("start init certificate info"); } }
package sun.flower.diver;import org.springframework.boot.Banner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication// 添加注解 @EnableDiscoveryClient,只有这样,服务注册、心跳检测相关配置信息才能被自动加载@EnableDiscoveryClient public class DiverApplication { public static void main(String[] args) { System.out.println("application start"); SpringApplication application = new SpringApplication(DiverApplication.class); // 添加监听器,此时监听器类不需要标注是一个Bean //application.addListeners(new BaseListener());  application.setBannerMode(Banner.Mode.OFF); application.run(args); System.out.println("application started"); } }

 

转载于:https://www.cnblogs.com/yang21/p/9865399.html

你可能感兴趣的文章
当Eclipse爱上SVN
查看>>
hdu 4586 Play the Dice (概率+等比数列)
查看>>
阿里云api调用做简单的cmdb
查看>>
软考笔记
查看>>
ORACLE 日期函数
查看>>
【Java基础总结】数据库编程
查看>>
SVN commit:remains in tree-conflict错误的解决办法
查看>>
PHP不使用内置函数intval(),实现字符串转整数
查看>>
HYSBZ 2243 染色 LCT学习
查看>>
Linux crontab命令详解与实例
查看>>
Log4j配置
查看>>
Swift与OC混编
查看>>
Image与Bitmap的区别及相互转换
查看>>
地图标注
查看>>
Messages
查看>>
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 外部服务调用、内部服务调用优化,面向服务化的...
查看>>
IT民工创业之殇---续1
查看>>
How to build missing PHP 5.3 extensions on CentOS 5.6
查看>>
python基础之常用关键字总结
查看>>
Rx.net 例子——(1)基础
查看>>