banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

【转载】六种方式,教你在SpringBoot初始化时搞点事情!

title: 【转载】六种方式,教你在 SpringBoot 初始化时搞点事情!
date: 2021-08-24 15:19:00
comment: false
toc: true
category:

  • Java
    tags:
  • 转载
  • Java
  • SpringBoot
  • 初始化
  • 方式
  • 方法
  • 容器
  • 刷新
  • 监听
  • 启动
  • 项目
  • 事件

本文转载自:六种方式,教你在 SpringBoot 初始化时搞点事情!


前言#

在实际工作中总是需要在项目启动时做一些初始化的操作,比如初始化线程池、提前加载好加密证书.......

那么经典问题来了,这也是面试官经常会问到的一个问题:有哪些手段在 Spring Boot 项目启动的时候做一些事情?

方法有很多种,下面介绍几种常见的方法。

image

1、监听容器刷新完成扩展点 ApplicationListener<ContextRefreshedEvent>#

ApplicationContext 事件机制是观察者设计模式实现的,通过 ApplicationEvent 和 ApplicationListener 这两个接口实现 ApplicationContext 的事件机制。

Spring 中一些内置的事件如下:

  1. ContextRefreshedEvent:ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext 接口中使用 refresh () 方法来发生。此处的初始化是指:所有的 Bean 被成功装载,后处理 Bean 被检测并激活,所有 Singleton Bean 被预实例化,ApplicationContext 容器已就绪可用。
  2. ContextStartedEvent:当使用 ConfigurableApplicationContext (ApplicationContext 子接口)接口中的 start () 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序。
  3. ContextStoppedEvent:当使用 ConfigurableApplicationContext 接口中的 stop () 停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作。
  4. ContextClosedEvent:当使用 ConfigurableApplicationContext 接口中的 close () 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。
  5. 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............................");
    }
}

高级玩法#

可以自定事件完成一些特定的需求,比如:邮件发送成功之后,做一些业务处理。

  1. 自定义 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
    }
    
  2. 自定义监听器,代码如下:

    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);
         }
       }
    }
    
  3. 发送邮件后,触发事件,代码如下:

    public class SpringTest {
       public static void main(String args[]){
         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
         //创建一个ApplicationEvent对象
         EmailEvent event = new EmailEvent("hello","[email protected]","This is a test");
         //主动触发该事件
         context.publishEvent(event);
       }
    }
    

2、SpringBootCommandLineRunner接口#

当容器初始化完成之后会调用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

以上命令中传入了三个参数,分别是aaabbbccc,这三个参数将会被run()方法接收到。如下图:

image

源码分析#

读过我的文章的铁粉都应该知道CommandLineRunner是如何执行的,原文:头秃系列,二十三张图带你从源码分析 Spring Boot 启动流程~

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

image

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

image

3、SpringBootApplicationRunner接口#

ApplicationRunnerCommandLineRunner都是 Spring Boot 提供的,相对于CommandLineRunner来说对于控制台传入的参数封装更好一些,可以通过键值对来获取指定的参数,比如--version=2.1.0

此时运行这个 jar 命令如下:

java -jar demo.jar --version=2.1.0 aaa bbb ccc

以上命令传入了四个参数,一个键值对version=2.1.0,另外三个是分别是aaabbbccc

同样可以通过@Order()指定优先级,如下代码:

@Component
@Slf4j
public class CustomApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.debug("控制台接收的参数:{},{},{}",args.getOptionNames(),args.getNonOptionArgs(),args.getSourceArgs());
    }
}

通过以上命令运行,结果如下图:

image

源码分析#

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

image

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初始化完成,调用...........");
    }
}

总结#

实现方案有很多,作者只是总结了常用的六种,学会的点个赞。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。