准确的说是 spring 容器实例化完成后,几种初始化的方式。为什么这么说呢?下看面示例:
@Slf4j@Componentpublic class InitBeanDemo {@Autowiredprivate Environment env;public InitBeanDemo() {log.info("DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
示例是在 bean 的构造方法里做一些初始化的工作,示例比较简单只做了日志打印。是理想很丰满,现实很骨感,报错了:Constructor threw exception; nested exception is java.lang.NullPointerException。
原因是,Environment 尚未初始化完成。
接下来我们来探索一下 有哪些初始化方式能满足上面示例的需求。
构造方法里初始化
可以正常运行,在所有初始化方式里执行时机最早。原理是在 InitBeanDemo 实例化前就实例化了 Environment。
@Componentpublic class InitBeanDemo {private final Environment env;@Autowiredpublic InitBeanDemo (Environment environment) {this.env = environment;log.info("Constructor DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("Constructor ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
常规三件套
常规三件套:@PostConstruct、InitializingBean、initMethod。 如果你愿意的话,三种方式可以在同一个 Bean 下同时使用,执行的优先级 @PostConstruct > InitializingBean > initMethod。
@PostConstruct 注解
在一个可以被扫描到 Bean 里,添加一个 public void xxx () 方法并加上 @PostConstruct 注解,方法里编写需要初始化的逻辑。
同一个应用程序里可以有多个 @PostConstruct 注解,同一个 Bean 里也可以有多个 @PostConstruct 注解。
@Slf4j@Componentpublic class InitBeanDemo {@Autowiredprivate Environment env;@PostConstructpublic void init() {log.info("@PostConstruct DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("@PostConstruct ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
实现 InitializingBean 接口
实现 InitializingBean 接口,在 afterPropertiesSet () 方法里编写需要初始化的逻辑。
同一个应用程序里可以有多个实现 InitializingBean 接口的类,执行时机会按类名的自然顺序排序。
@Slf4j@Componentpublic class InitBeanDemo implements InitializingBean {@Autowiredprivate Environment env;@Overridepublic void afterPropertiesSet() throws Exception {log.info("InitializingBean DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("InitializingBean ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
指定 Bean 的 initMethod 方法
使用 @Bean 注解的 initMethod 属性可用于 Bean 的初始化后执行的方法。initMethod 必须是 public void 的无参构造方法。
@Configurationpublic class InitBeanConfig {@Bean(initMethod="initMethod")public InitBeanDemo initBeanDemo () {return new InitBeanDemo();}}
等同于 在 XML 配置中的 init-method 属性:
<bean id="initBeanDemo" class="com.xxx.InitBeanDemo" init-method="initMethod"></bean>
自定义 ApplicationListener 监听
两种方式,一种实现接口,另一种使用注解。
实现 ApplicationListener 接口
监听 ContextRefreshedEvent 事件。
@Slf4j@Componentpublic class InitBeanDemo implements ApplicationListener<ContextRefreshedEvent>{@Autowiredprivate Environment env;@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {log.info("ApplicationListener DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("ApplicationListener ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
@EventListener 注释
方法参数里指定 ContextRefreshedEvent 事件。
@Slf4j@Componentpublic class InitBeanDemo {@Autowiredprivate Environment env;@EventListenerpublic void onApplicationEvent2(ContextRefreshedEvent event) {log.info("@EventListener DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("@EventListener ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
Spring Boot 提供的初始化接口
ApplicationRunner 接口
@Slf4j@Componentpublic class InitBeanDemo implements ApplicationRunner {@Autowiredprivate Environment env;@Overridepublic void run(ApplicationArguments args) throws Exception {log.info("ApplicationRunner: {}", args);log.info("ApplicationRunner: {}", args.getOptionNames());log.info("ApplicationRunner DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("ApplicationRunner ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
CommandLineRunner 接口
可以在同一个应用程序上下文中定义多个 CommandLineRunner bean,并且可以使用 @Ordered 接口或 @Order 注释进行排序。
@Slf4j@Componentpublic class InitBeanDemo implements CommandLineRunner {@Autowiredprivate Environment env;@Overridepublic void run(String... args) throws Exception {log.info("CommandLineRunner: {}", args);log.info("CommandLineRunner DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles()));log.info("CommandLineRunner ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles()));}}
在同一个 Bean 里使用以上初始化方式的执行先后顺序
在同一个 Bean 里使用以上初始化方式,运行的日志片段:
2021-06-07 11:24:41|INFO |main|c.c.s.s.t.ConstructorInitDemo|Constructor DefaultProfiles: [default]2021-06-07 11:24:41|INFO |main|c.c.s.s.t.ConstructorInitDemo|Constructor ActiveProfiles: [sit]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|@PostConstruct DefaultProfiles: [default]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|@PostConstruct ActiveProfiles: [sit]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|InitializingBean DefaultProfiles: [default]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|InitializingBean ActiveProfiles: [sit]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|initMethod DefaultProfiles: [default]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|initMethod ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|@EventListener DefaultProfiles: [default]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|@EventListener ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationListener DefaultProfiles: [default]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationListener ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner: org.springframework.boot.DefaultApplicationArguments@68bef3df2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner: []2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner DefaultProfiles: [default]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|CommandLineRunner: {}2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|CommandLineRunner DefaultProfiles: [default]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|CommandLineRunner ActiveProfiles: [sit]
也即 整篇文章整理的先后顺序。
————————————————
版权声明:本文为CSDN博主「pbxs」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/WLQ0621/article/details/117661645
