Spring Boot 如何处理项目启动时初始化资源
在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做少量初始化的操作,比方初始化线程池,提前加载好加密证书等。今天就给大家详情一个 Spring Boot 神器,专门帮助大家处理项目启动初始化资源操作。
这个神器就是 CommandLineRunner,CommandLineRunner 接口的 Component 会在所有 Spring Beans 都初始化之后,SpringApplication.run() 之前执行,非常适合在应用程序启动之初进行少量数据初始化的工作。
接下来我们就运用案例测试它如何使用,在测试之前在启动类加两行打印提醒,方便我们识别 CommandLineRunner 的执行时机。
@SpringBootApplicationpublic 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."); }}接下来我们直接创立一个类继承 CommandLineRunner ,并实现它的 run() 方法。
@Componentpublic class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize ..."); }}我们在 run() 方法中打印了少量参数来看出它的执行时机。完成之后启动项目进行测试:
...The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE)...2018-04-21 22:21:34.706 INFO 27016 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2018-04-21 22:21:34.710 INFO 27016 --- [ main] com.neo.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128)The Runner start to initialize ...The service has started.根据控制台的打印信息我们可以看出 CommandLineRunner 中的方法会在 Spring Boot 容器加载之后执行,执行完成后项目启动完成。
假如我们在启动容器的时候需要初始化很多资源,并且初始化资源相互之间有序,那如何保证不同的 CommandLineRunner 的执行顺序呢?Spring Boot 也给出理解决方案。那就是使用 @Order 注解。
我们创立两个 CommandLineRunner 的实现类来进行测试:
第一个实现类:
@Component@Order(1)public class OrderRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The OrderRunner1 start to initialize ..."); }}第二个实现类:
@Component@Order(2)public class OrderRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The OrderRunner2 start to initialize ..."); }}增加完成之后重新启动,观察执行顺序:
...The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE)...2018-04-21 22:21:34.706 INFO 27016 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2018-04-21 22:21:34.710 INFO 27016 --- [ main] com.neo.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128)The OrderRunner1 start to initialize ...The OrderRunner2 start to initialize ...The Runner start to initialize ...The service has started.通过控制台的输出我们发现,增加 @Order 注解的实现类最先执行,并且@Order()里面的值越小启动越早。
在实践中,使用ApplicationRunner也可以达到相同的目的,两着差别不大。看来使用 Spring Boot 处理初始化资源的问题非常简单。
本文的重点是你有没有收获与成长,其他的都不重要,希望读者们能谨记这一点。同时我经过多年的收藏目前也算收集到了一套完整的学习资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、Jvm性能调优、Spring,MyBatis,Nginx源码分析,Redis,ActiveMQ、、Mycat、Netty、Kafka、Mysql、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多个知识点高级进阶干货,希望对想成为架构师的朋友有肯定的参考和帮助
喜欢这篇文章的朋友可以点个喜欢,也可以关注一下我的个人专题:Java成长之路
需要更详细思维导图和以下资料的可以加一下技术交流分享群:“708 701 457”免费获取




1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Spring Boot 如何处理项目启动时初始化资源