内容简介:在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。今天就给大家介绍一个 Spring Boot 神器,专门帮助大家解决项目启动初始化资源操作。这个神器就是接下来我们就运用案例测试它如何使用,在测试之前在启动类加两行打印提示,方便我们识别
在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。今天就给大家介绍一个 Spring Boot 神器,专门帮助大家解决项目启动初始化资源操作。
这个神器就是 CommandLineRunner
, CommandLineRunner
接口的 Component
会在所有 Spring Beans 都初始化之后, SpringApplication.run()
之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作。
接下来我们就运用案例测试它如何使用,在测试之前在启动类加两行打印提示,方便我们识别 CommandLineRunner
的执行时机。
@SpringBootApplication
public 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()
方法。
@Component
public 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 也给出了解决方案。那就是使用 @Orderv
注解。
我们创建两个 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 解决初始化资源的问题非常简单。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- NSQ--nsqd初始化启动
- Android 应用启动性能:延迟初始化
- Composer 的 Autoload 源码实现——启动与初始化
- SpringBoot 启动分析(三) — Environment 的初始化流程
- Spring MVC源码(一) ----- 启动过程与组件初始化
- Activiti6.0教程(2) - 初始化表, 部署流程, 启动流程, 创建的表介绍
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
高效前端:Web高效编程与优化实践
李银城 著 / 机械工业出版社 / 2018-3-15 / 89.00元
这不是一本单纯讲解前端编程技巧的书,而是一本注重思想提升和内功修炼的书。 全书以问题为导向,精选了前端开发中的34个疑难问题,从分析问题的原因入手,逐步给出解决方案,并分析各种方案的优劣,最后针对每个问题总结出高效编程的最佳实践和各种性能优化的方法。 全书共7章,内容从逻辑上大致可以分为两大类: 第一类,偏向实践,围绕HTML、CSS、JavaScript等传统前端技术,以及PW......一起来看看 《高效前端:Web高效编程与优化实践》 这本书的介绍吧!