Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源
Spring Boot 2 (七):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 也给出了解决方案。那就是使用 @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 解决初始化资源的问题非常简单。
Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源的更多相关文章
- (转)Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源
http://www.ityouknow.com/springboot/2018/05/03/spring-boot-commandLineRunner.html 在我们实际工作中,总会遇到这样需求, ...
- Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源
在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等.今天就给大家介绍一个 Spring Boot 神器,专门帮助大家解决项目启动初始化资 ...
- Spring Boot 如何解决项目启动时初始化资源
在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等.今天就给大家介绍一个 Spring Boot 神器,专门帮助大家解决项目启动初始化资 ...
- SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据
在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationRunn ...
- 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean
让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...
- Spring Security 解析(七) —— Spring Security Oauth2 源码解析
Spring Security 解析(七) -- Spring Security Oauth2 源码解析 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因 ...
- 1.Spring项目启动时,加载相关初始化配置
Spring项目启动时,会加载一些常用的配置: 1.加载spring上下文 SpringApplicationContextUtils.initApplicationContext(event.get ...
- Spring Boot学习--项目启动时执行指定service的指定方法
Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...
- Spring Boot学习--项目启动时执行特定方法
Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动 ...
随机推荐
- 【LeetCode每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- trie字典树
---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 #include <bits/stdc++.h> usin ...
- DataFrame.nunique(),DataFrame.count()
1. nunique() DataFrame.nunique(axis = 0,dropna = True ) 功能:计算请求轴上的不同观察结果 参数: axis : {0或'index',1或'co ...
- 初尝Web API《转》
HTTP 并不是只能用在网页中.它其实还是一个强大的平台,可以用来生成一些API,暴露服务和数据.HTTP很简单灵活,还非常普及.几乎所有你能想到的平台都有HTTP库,所以HTTP服务可以囊括很大范围 ...
- C#使用HttpWebRequest与HttpWebResponse模拟用户登录
模拟艺龙旅游网登录 想模拟登录,首先整理一下流程 1.通过360浏览器(IE,火狐等等)F12开发人员工具抓到相关数据 2.获取验证码(拿到cookie),登录时也需要使用 3.登录 -------- ...
- php删除文件或文件夹
<?php function deleteDir($dir) { if (!$handle = @opendir($dir)) { return false; } while (false != ...
- Beta冲刺阶段3.0
1. 提供当天站立式会议照片一张 2. 每个人的工作 (有work item 的ID) 成员 昨天已完成的工作 今天计划完成的工作 工作中遇到的困难 具体贡献 郑晓丽 完成"我的活动&quo ...
- Gardener Bo (树剖 + 问题分解)
一开始没看懂计算答案的第四部和update2,很迷.然后一直推敲之后才发下我计算的时候漏掉一个关键点.没有把加值的影响放到父节点上. #include<bits/stdc++.h> #de ...
- 电脑已连接wifi的密码查询
有时候,想登陆自己家的无线网络(尤其朋友来家里突然要连接无线网络),脑子刹那间一片空白想不起来密码,怎么办呢? 其实,我们可以通过电脑来查看网络的密码,现在分享如何在笔记本电脑上查看连接过的无线网络密 ...
- 特征点方法 - Harris和SURF的手工实现
整理去年做的小项目,纪念我的图像处理入门. 因为要在DSP上实现,所以完全手工C代码垒起来的,还要保证和PC端跑的结果一样,觉得可能特殊场景会有用,上传github,没有依赖任何库: 格式注释什么的暂 ...