关于后端:六种方式教你在SpringBoot初始化时搞点事情

3次阅读

共计 4623 个字符,预计需要花费 12 分钟才能阅读完成。

前言
在理论工作中总是须要在我的项目启动时做一些初始化的操作,比方初始化线程池、提前加载好加密证书 …….
那么经典问题来了,这也是面试官常常会问到的一个问题:有哪些伎俩在 Spring Boot 我的项目启动的时候做一些事件?
办法有很多种,上面介绍几种常见的办法。

1、监听容器刷新实现扩大点 ApplicationListener<ContextRefreshedEvent>
ApplicationContext 事件机制是观察者设计模式实现的,通过 ApplicationEvent 和 ApplicationListener 这两个接口实现 ApplicationContext 的事件机制。
Spring 中一些内置的事件如下:

ContextRefreshedEvent:ApplicationContext 被初始化或刷新时,该事件被公布。这也能够在 ConfigurableApplicationContext 接口中应用 refresh() 办法来产生。此处的初始化是指:所有的 Bean 被胜利装载,后处理 Bean 被检测并激活,所有 Singleton Bean 被预实例化,ApplicationContext 容器已就绪可用。
ContextStartedEvent:当应用 ConfigurableApplicationContext(ApplicationContext 子接口)接口中的 start() 办法启动 ApplicationContext 时,该事件被公布。你能够考察你的数据库,或者你能够在承受到这个事件后重启任何进行的应用程序。
ContextStoppedEvent:当应用 ConfigurableApplicationContext 接口中的 stop() 进行 ApplicationContext 时,公布这个事件。你能够在承受到这个事件后做必要的清理的工作。
ContextClosedEvent:当应用 ConfigurableApplicationContext 接口中的 close() 办法敞开 ApplicationContext 时,该事件被公布。一个已敞开的上下文达到生命周期末端;它不能被刷新或重启。
RequestHandledEvent:这是一个 web-specific 事件,通知所有 bean HTTP 申请曾经被服务。只能利用于应用 DispatcherServlet 的 Web 利用。在应用 Spring 作为前端的 MVC 控制器时,当 Spring 解决用户申请完结后,零碎会主动触发该事件。

好了,理解下面这些内置事件后,咱们能够监听 ContextRefreshedEvent 在 Spring Boot 启动时实现一些操作,代码如下:
@Component
public class TestApplicationListener implements ApplicationListener<ContextRefreshedEvent>{

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {System.out.println(contextRefreshedEvent);
    System.out.println("TestApplicationListener............................");
}

}
复制代码
高级玩法
能够自定事件实现一些特定的需要,比方:邮件发送胜利之后,做一些业务解决。

自定义 EmailEvent,代码如下:

public class EmailEvent extends ApplicationEvent{
private String address;
private String text;
public EmailEvent(Object source, String address, String text){
super(source);
this.address = address;
this.text = text;
}
public EmailEvent(Object source) {
super(source);
}
//……address 和 text 的 setter、getter
}
复制代码

自定义监听器,代码如下:

public class EmailNotifier implements ApplicationListener{
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EmailEvent) {
EmailEvent emailEvent = (EmailEvent)event;
System.out.println(“ 邮件地址:” + emailEvent.getAddress());
System.our.println(“ 邮件内容:” + emailEvent.getText());
} else {
System.our.println(“ 容器自身事件:” + event);
}
}
}
复制代码

发送邮件后,触发事件,代码如下:

public class SpringTest {
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext(“bean.xml”);
// 创立一个 ApplicationEvent 对象
EmailEvent event = new EmailEvent(“hello”,”abc@163.com”,”This is a test”);
// 被动触发该事件
context.publishEvent(event);
}
}
复制代码
2、SpringBoot 的 CommandLineRunner 接口
当容器初始化实现之后会调用 CommandLineRunner 中的 run()办法,同样可能达到容器启动之后实现一些事件。这种形式和 ApplicationListener 相比更加灵便,如下:

不同的 CommandLineRunner 实现能够通过 @Order()指定执行程序
能够接管从控制台输出的参数。

上面自定义一个实现类,代码如下:
@Component
@Slf4j
public class CustomCommandLineRunner implements CommandLineRunner {

/**
 * @param args 接管控制台传入的参数
 */
@Override
public void run(String... args) throws Exception {log.debug("从控制台接管参数 >>>>"+ Arrays.asList(args));
}

}
复制代码
运行这个 jar,命令如下:
java -jar demo.jar aaa bbb ccc
复制代码
以上命令中传入了三个参数,别离是 aaa、bbb、ccc,这三个参数将会被 run()办法接管到。如下图:

源码剖析

Spring Boot 加载上下文的入口在 org.springframework.context.ConfigurableApplicationContext()这个办法中,如下图:

调用 CommandLineRunner 在 callRunners(context, applicationArguments); 这个办法中执行,源码如下图:

3、SpringBoot 的 ApplicationRunner 接口
ApplicationRunner 和 CommandLineRunner 都是 Spring Boot 提供的,绝对于 CommandLineRunner 来说对于控制台传入的参数封装更好一些,能够通过键值对来获取指定的参数,比方 –version=2.1.0。
此时运行这个 jar 命令如下:
java -jar demo.jar –version=2.1.0 aaa bbb ccc
复制代码
以上命令传入了四个参数,一个键值对 version=2.1.0,另外三个是别离是 aaa、bbb、ccc。
同样能够通过 @Order()指定优先级,如下代码:
@Component
@Slf4j
public class CustomApplicationRunner implements ApplicationRunner {

@Override
public void run(ApplicationArguments args) throws Exception {log.debug("控制台接管的参数:{},{},{}",args.getOptionNames(),args.getNonOptionArgs(),args.getSourceArgs());
}

}
复制代码
通过以上命令运行,后果如下图:

源码剖析
和 CommandLineRunner 一样,同样在 callRunners()这个办法中执行,源码如下图:

4、@PostConstruct 注解
前三种针对的是容器的初始化实现之后做的一些事件,@PostConstruct 这个注解是针对 Bean 的初始化实现之后做一些事件,比方注册一些监听器 …
@PostConstruct 注解个别放在 Bean 的办法上,一旦 Bean 初始化实现之后,将会调用这个办法,代码如下:
@Component
@Slf4j
public class SimpleExampleBean {

@PostConstruct
public void init(){log.debug("Bean 初始化实现,调用...........");
}

}
复制代码
5、@Bean 注解中指定初始化办法
这种形式和 @PostConstruct 比拟相似,同样是指定一个办法在 Bean 初始化实现之后调用。
新建一个 Bean,代码如下:
@Slf4j
public class SimpleExampleBean {

public void init(){log.debug("Bean 初始化实现,调用...........");
}

}
复制代码
在配置类中通过 @Bean 实例化这个 Bean,不过 @Bean 中的 initMethod 这个属性须要指定初始化之后须要执行的办法,如下:
@Bean(initMethod = “init”)

public SimpleExampleBean simpleExampleBean(){return new SimpleExampleBean();
}

复制代码
6、InitializingBean 接口
InitializingBean 的用法基本上与 @PostConstruct 统一,只不过相应的 Bean 须要实现 afterPropertiesSet 办法,代码如下:
@Slf4j
@Component
public class SimpleExampleBean implements InitializingBean {

@Override
public void afterPropertiesSet()  {log.debug("Bean 初始化实现,调用...........");
}

}
复制代码
总结
实现计划有很多,作者只是总结了罕用的六种,学会的点个赞。

正文完
 0