【无标题】

目录

  • 前言
  • CommandLineRunner
  • ApplicationRunner
  • ApplicationListener
  • @PostConstruct
  • InitializationBean
  • 总结

前言

我们经常有一些业务需求,需要在项目启动后执行相关的业务代码,如:数据的初始化业务。今天我们来梳理一下有哪些方案?

CommandLineRunner

CommandLineRunner是一个接口,通过实现它,我们可以在Spring应用成功启动之后执行一些代码片段

我们先定义个User实体Bean

下面我们定义一个类实现CommandLineRunner接口

当 Spring Boot 在应用上下文中找到 CommandLineRunner bean,它将会在应用成功启动之后调用 run() 方法,并传递用于启动应用程序的命令行参数

java -jar demo-0.0.1-SNAPSHOT.jar --foo=bar --name=gujch

启动执行结果

小结:

  1. 命令行传入的参数并没有被解析,而只是显示出我们传入的字符串内容 --foo=bar,--name=gujch
  2. 在重写的 run() 方法上有 throws Exception 标记,Spring Boot 会将 CommandLineRunner 作为应用启动的一部分,如果运行 run() 方法时抛出 Exception,应用将会终止启动
  3. 当有多个 CommandLineRunner 时,将会按照 @Order 注解中的数字从小到大

如果我们只是想简单的获取以空格分隔的命令行参数,那 MyCommandLineRunner 就足够使用了

ApplicationRunner

上面提到,通过命令行启动并传递参数,MyCommandLineRunner 不能解析参数,如果要解析参数,那我们就要用到 ApplicationRunner 参数了

执行结果

到这里我们可以看出:

同 MyCommandLineRunner 相似,但 ApplicationRunner 可以通过 run 方法的 ApplicationArguments 对象解析出命令行参数,并且每个参数可以有多个值在里面,因为 getOptionValues 方法返回 List数组

在重写的 run() 方法上有 throws Exception 标记,Spring Boot 会将 ApplicationRunner 作为应用启动的一部分,如果运行 run() 方法时抛出 Exception,应用将会终止启动

ApplicationRunner 也可以使用 @Order 注解进行排序,从启动结果来看,它与 CommandLineRunner 共享 order 的顺序

我们来看看源码,CommandLineRunner 和 ApplicationRunner 是在何时被调用的呢?

SpringApplication.java类中callRunners方法

上面可以看到spring获取CommandLineRunner 和 ApplicationRunner Bean会放到List中,然后一起排序,所以@Order排序是共享的

ApplicationListener

如果我们不需要获取命令行参数时,我们可以将启动逻辑绑定到 Spring 的 ApplicationReadyEvent 上

执行结果

ApplicationReadyEvent 当且仅当 在应用程序就绪之后才被触发。

启动顺序Order不与CommandLineRunner和ApplicationRunner共享

如果我们不需要获取命令行参数,我们可以通过 ApplicationListener 创建一些全局的启动逻辑,我们还可以通过它获取 Spring Boot 支持的 configuration properties 环境变量参数 ,因为event参数有configuration上下文

@PostConstruct

创建启动逻辑的另一种简单解决方案是提供一种在 bean 创建期间由 Spring 调用的初始化方法。我们要做的就只是将 @PostConstruct 注解添加到方法中:

执行结果

从上面运行结果可以看出:

1)Spring 创建完 bean之后 (在启动之前),便会立即调用 @PostConstruct 注解标记的方法,因此我们无法使用 @Order 注解对其进行自由排序,因为它可能依赖于 @Autowired插入到我们 bean 中的其他 Spring bean。

2)相反,它将在依赖于它的所有 bean 被初始化之后被调用

@PostConstruct 方法固有地绑定到现有的 Spring bean,因此应仅将其用于此单个 bean 的初始化逻辑;

@PostConstruct应用场景:

在生成对象时候做一些初始化操作,而这些初始化操作又依赖于依赖注入(populateBean),那么就无法在构造函数中实现。这时,可以使用@PostConstruct注解一个方法来完成初始化,@PostConstruct注解的方法将会在依赖注入完成后被自动调用。

InitializingBean

与 @PostConstruct 解决方案非常相似,我们可以实现 InitializingBean 接口,并让 Spring 调用某个初始化方法:

执行结果

@PostConstruct 和 afterPropertiesSet 区别

1、afterPropertiesSet,顾名思义「在属性设置之后」,调用该方法时,该 bean 的所有属性已经被 Spring 填充。如果我们在某些属性上使用 @Autowired(常规操作应该使用构造函数注入),那么 Spring 将在调用afterPropertiesSet 之前将 bean 注入这些属性。但 @PostConstruct 并没有这些属性填充限制

2、所以

InitializingBean.afterPropertiesSet 解决方案比使用 @PostConstruct 更安全,因为如果我们依赖尚未自动注入的 @Autowired 字段,则 @PostConstruct 方法可能会遇到 NullPointerExceptions

总结

从上面的例子中我们就可以发现各个启动方案的顺序

针对Bean实体启动初始化 顺序

Construct >> @Autowired(依赖注入) >> @postConstruct >> InitializingBean

针对整体项目启动 顺序

CommandLineRunner和ApplicationRunner >> ApplicationListener

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值