springboot2启动时执行,初始化(或定时任务)servletContext问题

2023-01-12 11:07:56

目录springboot2启动时执行,初始化(或定时任务)servletContext可以实现ApplicationListener使用注解注入springboot启动时初始化数据的几种方式一、Ap...

目录
springboot2启动时执行,初始化(或定时任务)servletContext
可以实现 ApplicationListener
使用注解注入
springboot启动时初始化数据的几种方式
一、ApplicationRunner与CommandLineRunner
二、Spring Bean初始化的InitializingBean,init-method和PostConstruct
三、Spring的事件机制
总结

springboot2启动时执行,初始化(或定时任务)servletContext

需求:springboot 启动后自动执行,初始化数据,并将数据放到 servletContext 中。

首先,不可使用 ServletContextListener 即不能用 @WebListener ,因为 servlet 容器初始化后,spring 并未初始化完毕,不能使用 @Autowired 注入 spring 的对象。

代码如下:

可以实现 ApplicationListener

@Component
public class SettingDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
  @Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    WebApplicationContext webApplicationContext =
      (WebApplicationContext)contextRefreshedEvent.getApplicationContext();
    ServletContext servletContext = webApplicationContext.getServletContext();
    servletContext.setAttribute("key", "value");
  }
}

使用注解注入

service类里注入 servletContext

@Autowired private ServletContext servletContext;

service类里要启动执行的方法加上注解

@PostConstruct

在定时任务里,也可以注入 servletContext,进行定时操作

@Component
public class MyTask {
  @Autowired
  private ServletContext servletContext;

  // 秒 分 时 日 月 周
  @Scheduled(cron = "0 * * * * *")
  public void resetDays() {
    // servletContext
  }
}

springboot启动时初始化数据的几种方式

在我们用springboot搭建项目的时候,经常碰到在项目启动时初始化一些字典数据、地市数据、等各类需求,针对这种需求Spring与Spring boot为我们提供了以下几种方案供我们选择:

springboot提供的ApplicationRunner与CommandLineRunner接口
Spring Bean初始化的init-method、PostConstruct注解、

InitializingBean、BeanPostProcessor接口

Spring的事件机制: 实现 ApplicationListener 接口

一、ApplicationRunner与CommandLineRunner

如果需要在SpringApplication启动时执行一些特殊的代码,可以通过实现ApplicationRunner或CommandLineRunner接口,这两个接口都提供单一的run方法,且run方法仅在SpringApplication.run(…)完成之前调用。

区别:参数不一样,CommandLineRunner的参数是最原始的参数,没有进行任何处理,ApplicationRunner的参数是ApplicationArguments

ApplicationRunner接口只需要自己创建类实现ApplicationRunner接口

/**
* @author 重庆阿汤哥
* @Description: 测试
* @date 2021/11/26 
*/
@Component
@Slf4j
public class ApplicationRunnerTest implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    log.info("ApplicationRunner init data.....");
  }
}

CommandLineRunner

对于这个接口而言,我们可以通过Order注解或者使用Ordered接口来指定调用顺序,@Order()中的值越小,优先级越高

/**
* @author 重庆阿汤哥
* @Description: 测试
* @date 2021/11/26 
*/
@Component
@Slf4j
@Order(1)
public class CommandLineRunnerTest implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    log.info("CommandLineRunner init data.....");
  }
}

二、Spring Bean初始化的InitializingBean,init-method和PostConstruct

InitializingBean接口、BeanPostProcessor接口

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSpythonet()方法。

在spring初始化bean的时候,如果bean实现了InitializingBean接口,在对象的所有属性被初始化后之后才会调用afterPropertiesSet()方法

/**
* @author 重庆阿汤哥
* @Description: 测试
* @date 2021/11/26 
*/
@Component
@Slf4j

@Component
public class InitialingzingBeanTest implements InitializingBean {

  @Override
  public void afterPropertiesSet() throws Exception {
   log.info("InitializingBean init....");
  }
}

@PostConstruct

/**
* @author 重庆阿汤哥
* @Description: 测试
* @date 2021/11/26 
*/
@Component
@Slf4j
public class DynamicRouteMonitor {
 
  @PostConstruct
  public void init() {
    log.info("gateway route init...");
    }
}

BeanPostProcessor接口

可以用于判断某些特定类加载完成后才能初始化数据的场景,只需要自己实现该接口中的方法进行前置条件判断

public interface BeanPostProcessor {
  @Nullable
  default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    return bean;
  }

  @Nullable
  default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    return bean;
  }
}

三、Spring的事件机制

在Spring中,默认对ApplicationEvent事件提供了如下支持:

ContextStartedEvent:ApplicationCpythonontext启动后触发的事件
ContextStoppedEvent:ApplicationContext停止后触发的事件
ContextRefreshedEvent:ApplicationContext初始化或刷新完成后触发的事件;也就是容器初始化完成后调用。
ContextClosedEvent:ApplicationContext关闭后触发的事件;如web容器关闭时自动会触发spring容器的关闭。甚至大家听说过的钩子程序都是调用ctx.registerShutdownHook()进行注册虚拟机关闭。

利用ContextRefreshedEvent事件进行初始化操作

/**
* @author 重庆阿汤哥
* @Description:容器初始化完整后初始字典数据
* @date 2021/11/26 10:51
*/
@Component
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
  public void onApplicationEvent(ContextRefreshedEvent event) {
    //在容器加载完毕后获取dao层来操作数据库
    ISysDictTypeService sysDictTypeService = (ISysDictTypeService) event.getApplicationContext().getBean(ISysDictTypeService.class);

    sysDictTypeService.initDict();

    IProvCityService provCityService = (IProvCityService) event.getApplicationContext().getBean(IProvCityService.class);
    provCityService.initProvCity();

  }

  @Override
  protected Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}

这几种方式都可以满足我们日常开发的需求,针对具体场景使用对应的方案,在微服务应用中使用也较广泛。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。