SpringBoot系列: CommandLineRunner接口的用处
========================================
使用 CommandLineRunner 对Spring Bean进行额外初始化
========================================
如果想要在Spring 容器初始化做一些额外的工作, 比如要对Spring Bean 对象做一些额外的工作, 首先想到的方式是, 直接将代码写在 main() 函数的 SpringApplication.run()后, 比如:
@SpringBootApplication
public class PebbleDemoApplication { public static void main(String[] args) throws IOException {
SpringApplication.run(PebbleDemoApplication.class, args); //done something here
}
}
其实, 这样的方式的方式不行的, 在main()方法中, 要想访问到Spring 中的 bean 对象, 并不容易.有两个方法:
方法1: 入口类中, 新增一个函数, 打上注解 @PostConstruct , 则这个函数在入口类初始化完成后被调用.
方法2: Spring Boot 为我们提供了更好的方式, 即声明我们自己的 CommandLineRunner Bean.
具体为: 新建类去实现 CommandLineRunner 接口, 同时为类加上 @Component 注解.
当Spring 容器初始化完成后, Spring 会遍历所有实现 CommandLineRunner 接口的类, 并运行其run() 方法.
这个方式是最推荐的, 原因是:
1. 因为 Runner 类也是 @Component 类, 这样就能利用上Spring的依赖注入, 获取到 Spring 管理的bean对象.
2. 可以创建多个 Runner 类, 为了控制执行顺序, 可以加上 @Order 注解, 序号越小越早执行.
下面代码打印文本的顺序是: step 1 -> step 3 -> step 4 -> step 5
@SpringBootApplication
public class PebbleDemoApplication { public static void main(String[] args) throws IOException {
System.out.println("Step 1: The service will start"); //step 1
SpringApplication.run(PebbleDemoApplication.class, args); //step 2
System.out.println("Step 5: The service has started"); //step 5
}
} @Component
@Order(1)
class Runner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Step 3: The Runner1 run ...");
}
} @Component
@Order(2)
class Runner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Step 4: The Runner2 run ...");
}
}
========================================
使用 CommandLineRunner 创建纯粹的命令行程序
========================================
步骤:
1. pom.xml 中要将 spring-boot-starter-web 依赖去除, 换上 spring-boot-starter 基础依赖包.
2. 按照上面的方式3, 新建 CommandLineRunner 类, 并声明为 @Component.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
=======================================
参考
=======================================
http://www.ityouknow.com/springboot/2018/05/03/spring-boot-commandLineRunner.html
SpringBoot系列: CommandLineRunner接口的用处的更多相关文章
- Springboot学习—CommandLineRunner接口(转载)
前言 Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次. 如何使用CommandLineR ...
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...
- SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口
前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...
- Springboot系列(七) 集成接口文档swagger,使用,测试
Springboot 配置接口文档swagger 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...
- springBoot系列-->springBoot注解大全
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”
前言 通过之前的博客:SpringBoot系列——jar包与war包的部署,我们已经知道了如果实现项目的简单部署,但项目部署的时候最烦的是什么?修改成发布环境对应的配置!数据库连接地址.Eureka注 ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...
- SpringBoot系列——Spring-Data-JPA
前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...
随机推荐
- Linux Collection:系统信息和配置
PAS Linux系统配置相关文件 /etc 目录中是系统和各类软件的配置文件 /var/log 系统日志 /proc 系统运行信息 PAS /boot空间不足 查看内核 dpkg --get-sel ...
- 一位月薪1.2w的北漂程序员真实生活!
“ 每个人都有一条生活道路.千万人,千万条,各不相同,各有特点.但是并不是好人都有一条好路,坏人都有一坏路.有的时候却恰恰相反.这虽然不是历史的必然,但却是客观社会存在的.今天咱们说的这部书是发生在当 ...
- C# 使用DES对字符串进行加密
1.DES加密是属于对称加密,加密和解密使用的密钥必须要保持一致,且必须为8位,使用前首先添加引用: 2.逻辑实现代码如下:
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- spring+struts2+hibernate框架搭建(Maven工程)
搭建Spring 1.porm.xml中添加jar包 <!-- spring3 --> <dependency> <groupId>org.springframew ...
- .Net Core应用框架Util介绍(一)
距离上次发文,已经过去了三年半,这几年技术更新节奏异常迅猛,.Net进入了跨平台时代,前端也被革命性的颠覆. 回顾 2015年,正当我还沉迷于JQuery + EasyUi的封装时,突然意识到技术已经 ...
- OC调用c++函数
1.调用的时候我明明改成了 .mm , 也添加了libstdc++.dylib 调用自己(xcode )写的(cocoa static lib )c++ ,编译总是报找不到库里的函数, 最后我在 ...
- Linux(Ubuntu 16) 下Java开发环境的配置(二)------Tomcat的配置及常见问题
前言 相比于java JDK的配置,Tomcat的配置简单的多,简直就相当于直接运行了,本文以Tomcat8.0为例进行配置 1.Tomcat的下载 地址:https://tomcat.apach ...
- Java 常用数据结构对象的实现原理 集合类 List Set Map 哪些线程安全 (美团面试题目)
Java中的集合包括三大类,它们是Set.List和Map, 它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现类. List.Set都继承自Collection接口 ...
- QTP自动化测试-打开运行报告
automation菜单下-点击 result