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提供了初始化方法的方式,它只包括afterPropertiesSet()方法。

在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:ApplicationContext启动后触发的事件
- 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();
    }
}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值