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:/ ...
随机推荐
- C# for循环或者foreach往List中添加对象的时候前面的数据总被最后加入的覆盖
昨天我旁边小姐姐遇到一个问题,就是在执行for循环往list添加数据的时候,前面的数据信息总是被后面的数据信息所覆盖. 这样编写就会造成这样的数据效果:(所有的数据都会被覆盖) 问题原因:对 ...
- zabbix源码安装 令人窒息的操作
一.简介 zabbix-server主要分为2部分: zabbix程序 程序根据客户端的监控项,从客户端获取数据并写入到数据库,再根据触发器/动作等配置进行操作. 展示页面 使用php编写,php脚本 ...
- 2019-04-15 python深浅复制
原作地址:https://www.cnblogs.com/xueli/p/4952063.html 在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,pyth ...
- 引用传递this关键字
this关键字主要有三个应用: (1)this调用本类中的属性,也就是类中的成员变量: (2)this调用本类中的其他方法: (3)this调用本类中的其他构造方法,调用时要放在构造方法的首行.
- python 角度和弧度转化
>>> import math >>> math.degrees(math.pi/) 90.0 >>> math.radians() 1.5707 ...
- Codeforces Round 1152 (div. 2)
奇差.ABC三题,排名400. 首先是策略问题. 由于第一眼看到D的时候感觉不太会做,于是去看E. 一看到E这不欧拉回路吗,可做可做, 于是--我不会欧拉回路! 手推.推了半天啥也没弄出来, 于是大概 ...
- iOS开发基础-KVC简单介绍
一.KVC介绍 键值编码(Key Value Coding,KVC):使开发者不必进行任何操作就可以进行属性的动态读写. KVC操作方法由 NSKeyValueCoding 协议提供,而 NSObje ...
- matplotlib绘图的基本操作
转自:Laumians博客园 更简明易懂看Matplotlib Python 画图教程 (莫烦Python)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili. ...
- java高级-动态注入替换类Instrumentation
介绍 利用java.lang.instrument(容器类) 做动态 Instrumentation(执行容器) 是 Java SE 5 的新特性. 使用 Instrumentation,开发者可以构 ...
- 家庭记账本小程序之改(java web基础版五)
实现修改消费账单 1.main_left.jsp中该部分,调用Servlet中的list方法 2.Servlet中的list方法,调用Dao层的list方法,跳转到list.jsp页面 3.Dao层的 ...