SpringBoot初始化监听事件

1. 业务概述

在实际业务开发中经常需要在项目启动的时候加载一些初始化操作,比如初始化线程池,加密证书,数据库监听等等,本文就是基于springboot通过启动等监听事件实现一些操作。

2. 监听方式

2.1 ApplicationListener监听器

监听容器刷新完成扩展点ApplicationListener

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处理用户请求结束后,系统会自动触发该事件。

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.RequestHandledEvent;

/**
 * 1、监听容器刷新完成扩展点
 * ApplicationListener<ContextRefreshedEvent>
 * 监听ContextRefreshedEvent在Spring Boot 启动时完成一些操作
 *
 * @author zrj
 * @since 2021/9/1
 **/
@Component
public class TestApplicationListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        //ContextRefreshedEvent:ApplicationContext 被初始化或刷新时,该事件被发布。
        if (event instanceof ContextRefreshedEvent) {
            System.out.println("ContextRefreshedEvent事件:" + event);
        } else if (event instanceof ContextStartedEvent) {
            //ContextStartedEvent:当使用 ConfigurableApplicationContext (ApplicationContext子接口)接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。
            System.out.println("ContextStartedEvent事件:" + event);
        } else if (event instanceof ContextStoppedEvent) {
            //ContextStoppedEvent:当使用 ConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 时,发布这个事件。
            System.out.println("ContextStoppedEvent事件:" + event);
        } else if (event instanceof ContextClosedEvent) {
            //ContextClosedEvent:当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。
            System.out.println("ContextClosedEvent事件:" + event);
        } else if (event instanceof RequestHandledEvent) {
            //RequestHandledEvent:这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。只能应用于使用DispatcherServlet的Web应用。
            System.out.println("RequestHandledEvent事件:" + event);
        } else {
            System.out.println("[TestApplicationListener]容器本身事件:" + event);
        }
    }
}

2.2 自定义监听器

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

2.2.1. 自定义EmailEvent,代码如下:

import org.springframework.context.ApplicationEvent;

import java.time.Clock;

/**
 * 自定义EmailEvent
 * 可以自定事件完成一些特定的需求,比如:邮件发送成功之后,做一些业务处理。
 *
 * @author zrj
 * @since 2021/9/1
 **/
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;
        System.out.println("啥也不说,发送邮件...");
    }

    public EmailEvent(Object source) {
        super(source);
    }

    public EmailEvent(Object source, Clock clock) {
        super(source, clock);
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

2.2.2.自定义监听器

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 自定义监听器
 *
 * @author zrj
 * @since 2021/9/1
 **/
@Component
public class EmailListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof EmailEvent) {
            EmailEvent emailEvent = (EmailEvent) event;
            System.out.println("[EmailEvent]邮件地址:" + emailEvent.getAddress());
            System.out.println("[EmailEvent]邮件内容:" + emailEvent.getText());
        } else {
            //System.out.println("[EmailEvent]容器本身事件:" + event);
        }
    }
}

2.2.3.ApplicationContxt上下文环境获取


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring上下文环境获取
 *
 * @author zrj
 * @since 2021/9/1
 **/
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

2.2.4.controller控制器

import com.zrj.unit.listener.ApplicationContextProvider;
import com.zrj.unit.listener.EmailEvent;
import com.zrj.unit.listener.PostConstructBean;
import com.zrj.unit.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zrj
 * @since 2021/8/30
 **/
@RestController
@RequestMapping("/mail")
public class MailEventController {

    @Resource
    private UserService userService;
    @Resource
    private ApplicationContextProvider provider;
    @Resource
    private PostConstructBean postConstructBean;
    //@Resource
    //private ApplicationContext context;


    @GetMapping("/test")
    public String test() {
        return userService.getUser();
    }

    @GetMapping("/bean")
    public String bean() {
        postConstructBean.initMethod();
        return "bean init";
    }

    /**
     * 发送邮件触发事件
     */
    @GetMapping("/send")
    public String SendMailEvent() {
        ApplicationContext context = provider.getApplicationContext();
        //创建一个ApplicationEvent对象
        EmailEvent event = new EmailEvent("hello", "abc@163.com", "This is a test");
        //主动触发该事件
        context.publishEvent(event);

        return "发送邮件触发事件";
    }
}

2.3 CommandLineRunner接口

当容器初始化完成之后会调用CommandLineRunner中的run()方法,同样能够达到容器启动之后完成一些事情。这种方式和ApplicationListener相比更加灵活,如下:

不同的CommandLineRunner实现可以通过@Order()指定执行顺序

2.3.1 idea配置启动接收参数。

一、VM options
VM options其实就是我们在程序中需要的运行时环境变量,它需要以-D或-X或-XX开头,每个参数使用空格分隔
使用最多的就是-Dkey=value设定系统属性值,比如-Dspring.profiles.active=dev3

二、Program arguments
Program arguments为我们传入main方法的字符串数组args[],它通常以--开头,如--spring.profiles.active=dev3
等价于-Dspring.profiles.active=dev3如果同时存在,以Program arguments配置优先

三、Environment variables
Environment variables没有前缀,优先级低于VM options,即如果VM options有一个变量和Environment variable中的变量的key相同,则以VM options中为准。

在这里插入图片描述

2.3.2 代码实现

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 2、SpringBoot的CommandLineRunner接口
 *
 * @author zrj
 * @since 2021/9/1
 **/
@Slf4j
@Component
public class CustomCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        log.info("从控制台接收参数>>>>" + Arrays.asList(args));
    }
}

2.3.3 源码分析

SpringApplication.run( UnitTestApplication.class, args );
在这里插入图片描述

2.4 ApplicationRunner接口

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

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 3、SpringBoot的ApplicationRunner接口
 *
 * @author zrj
 * @since 2021/9/1
 **/
@Slf4j
@Component
public class CustomApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.debug("控制台接收的参数:{},{},{}", args.getOptionNames(), args.getNonOptionArgs(), args.getSourceArgs());
    }
}

2.5 @PostConstruct注解

@PostConstruct这个注解是针对Bean的初始化完成之后做一些事情,比如注册一些监听器…
@PostConstruct注解一般放在Bean的方法上,一旦Bean初始化完成之后,将会调用这个方法

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 5、@Bean注解中指定初始化方法
 *
 * @author zrj
 * @since 2021/9/1
 **/
@Slf4j
@Component
public class PostConstructBean {
    @PostConstruct
    public void init() {
        log.debug("Bean初始化完成,调用init...........");
    }

    public void initMethod() {
        log.debug("Bean初始化完成,调用initMethod...........");
    }
}

2.6 @Bean注解中指定初始化方法

这种方式和@PostConstruct比较类似,同样是指定一个方法在Bean初始化完成之后调用。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zrj
 * @since 2021/9/1
 **/
@Configuration
public class ListenerConfig {

    @Bean(initMethod = "init")
    public PostConstructBean postConstructBean() {
        return new PostConstructBean();
    }
}

2.7 InitializingBean接口

InitializingBean的用法基本上与@PostConstruct一致,只不过相应的Bean需要实现afterPropertiesSet方法.

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @author zrj
 * @since 2021/9/1
 **/
@Slf4j
@Component
public class InitializingBeanTest implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        log.debug("Bean初始化完成,调用...........");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值