========================================
使用 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 1SpringApplication.run(PebbleDemoApplication.class, args);  //step 2System.out.println("Step 5: The service has started");     //step 5
    }
}@Component
@Order(1)
class Runner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("Step 3: The Runner1 run ...");}
}@Component
@Order(2)
class Runner2 implements CommandLineRunner {@Overridepublic 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

转载于:https://www.cnblogs.com/harrychinese/p/SpringBoot_CommandLineRunner.html

SpringBoot系列: CommandLineRunner接口的用处相关推荐

  1. SpringBoot中实现CommandLineRunner接口在项目启动后立即执行某方法

    场景 在启动SpringBoot项目的启动类之后需要其立即执行某方法. 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取 ...

  2. SpringBoot 系列教程(八十五):Spring Boot使用MD5加盐验签Api接口之前后端分离架构设计

    加密算法参考: 浅谈常见的七种加密算法及实现 加密算法参考: 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用 目的: 通过对API接口请求报文签名,后端进行验签处理, ...

  3. Springboot系列之Shiro、JWT、Redis 进行认证鉴权

    Springboot系列之Shiro.JWT.Redis 进行认证鉴权 Shiro架构 Apache Shiro是一个轻量级的安全框架 Shiro可以非常容易的开发出足够好的应用,其不仅可以用在Jav ...

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

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

  5. SpringBoot 整合 Spring Security 实现安全认证【SpringBoot系列9】

    SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见. 程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCl ...

  6. SpringBoot RabbitMQ 商品秒杀【SpringBoot系列15】

    SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见. 程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCl ...

  7. 《SpringBoot系列十六》条件装配时ConfigurationCondition和Condition有什么区别?什么时候用ConfigurationCondition?

    一.前言 针对条件装配我们讨论了如下内容: <SpringBoot系列十一>:精讲如何使用@Conditional系列注解做条件装配 <SpringBoot系列十二>:如何自定 ...

  8. C#进阶系列——WebApi 接口参数不再困惑:传参详解

    看这边文章时的疑惑是:WebApi中的参数加了[FromBody],不知所以然,就百度了下,看到了以下文章,和大家分享下: 原文链接:http://www.cnblogs.com/landeanfen ...

  9. SpringBoot系列: RestTemplate 快速入门

    ==================================== 相关的文章 ==================================== SpringBoot系列: 与Sprin ...

最新文章

  1. html回顾随笔JS(*^__^*)
  2. Unity3d 去掉exe版本的边框
  3. VTK修炼之道24:图像基本操作_单颜色通道图像合成彩色
  4. 拖动效果,防止选中文字兼容代码
  5. 理解 JavaScript 闭包
  6. 关于ubuntu环境下gcc使用的几点说明
  7. Swift - 数组排序方法(附样例)
  8. Maven私服的配置和使用
  9. 访问服务器的速度由哪些因素决定?
  10. gispython定义查询_定义查询方法
  11. 485. 最大连续 1 的个数
  12. 带你玩转 3D 检测和分割(一):MMDetection3D 整体框架介绍
  13. 假设检验一小些知识(单个正态总体均值的假设检验)
  14. sqlserver如何把两张表的数据合并为一张表
  15. Docker内时区查询和修改方法
  16. 企业信息化战略与实施
  17. 手机开机接入网络的过程
  18. 数商云:电商倒逼中药材专业市场交易,数字化助力中医药传承创新
  19. 网站SEO关键词排名提升的技巧
  20. vcruntime140_1.dll丢失的解决办法

热门文章

  1. JQuery实现——黑客帝国代码雨效果
  2. linux 文件格式压缩
  3. AE安装部署以及监测ArcEngine runtime 9.3是否安装
  4. ILSpy-替代.Net Reflector的工具
  5. Flutter CupertinoSlidingSegmentedControl 分段组件使用解析
  6. Consider defining a bean named 'entityManagerFactory' in your configuration解决办法
  7. iOS - AutoLayout
  8. jQuery之全选功能
  9. cocos2dx打飞机项目笔记六:GameScene类和碰撞检测 boundingbox
  10. Vue「六」前端路由、vue-router