========================================
使用 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接口的用处的更多相关文章

  1. Springboot学习—CommandLineRunner接口(转载)

    前言 Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次. 如何使用CommandLineR ...

  2. SpringBoot之CommandLineRunner接口和ApplicationRunner接口

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...

  3. SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口

    前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...

  4. Springboot系列(七) 集成接口文档swagger,使用,测试

    Springboot 配置接口文档swagger 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...

  5. springBoot系列-->springBoot注解大全

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  6. SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”

    前言 通过之前的博客:SpringBoot系列——jar包与war包的部署,我们已经知道了如果实现项目的简单部署,但项目部署的时候最烦的是什么?修改成发布环境对应的配置!数据库连接地址.Eureka注 ...

  7. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  8. Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控

    前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...

  9. SpringBoot系列——Spring-Data-JPA

    前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...

随机推荐

  1. LVS负载均衡基础介绍及NET、DR模式配置

    LVS:术语: CIP:Client IP:客户端IP: VIP:Virtual Server IP:虚拟主机对外IP: RIP:Real Server IP:真实主机IP: DIP:Director ...

  2. python进阶之time模块详解

    Time模块 Time模块包含的函数 Time模块包含了一下内置的函数,既有时间处理的,也有转换时间格式的: 序号 函数及描述 1 time.altzone 返回格林威治西部的夏令时地区的偏移秒数.如 ...

  3. 二叉搜索树的最近公共祖先的golang实现

    给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...

  4. Django组件--forms组件(注册用)

    一.forms组件--校验类的使用 二.form组件--校验类的参数 三.forms组件校验的局部钩子--自定义校验规则(要看源码理解) 四.forms组件校验的全局钩子--校验form表单两次密码输 ...

  5. socket接收大数据流

    客户端: import socket client = socket.socket() client.connect(("127.0.0.1", 9999)) while True ...

  6. 新数据革命: 开源C#图形化爬虫引擎Hawk5发布

    https://ferventdesert.github.io/Hawk/ Hawk是一款由沙漠之鹰历时五年个人业余时间开发的,开源图形化爬虫和数据清洗工具,GitHub Star超过2k+,前几代版 ...

  7. vue 组件中的钩子函数 不能直接写this

    export default { data(){ return { num: 18 } }, beforeRouteEnter(to, from, next){ next(vm=>{ vm.nu ...

  8. python学习之类的反射

    在学习网络编程的时候用到反射,然后发现自己反射这部分的应用不是很熟练,决定返回来重新整理一下 对于类的反射,主要有四个用法,下面都说一下 1.hasattr 判断对象或者类是否存在指定的属性,看代码以 ...

  9. K8S集群技术

    1.快速部署K8S环境 k8s-m :10.0.0.11   k8s-n1 :10.0.0.12   k8s-n2 :10.0.0.13 2.所有节点安装docker环境及依赖 2.1 上传docke ...

  10. Pandas 把数据写入csv

    Pandas 把数据写入csv from sklearn import datasets import pandas as pd iris = datasets.load_iris() iris_X ...