SpringBoot项目经常遇到启动项目的过程中需要初始化一些数据,比如字典,前置条件数据之类的需求,这个时候需要在项目启动的过程中去做这一块的工作。下面根据经验总结了以下几种项目启动的时候初始化数据的方法,可以选择一个自己喜欢的,从项目启动运行可以看出来他们的加载顺序,在这里不对加载顺序做解释。
以下代码运行在jdk17,SpringBoot3.0.2环境下
先准备下面的接口和服务类,做三层业务逻辑开发的同学都懂
publicinterfaceInitService { voidinitData(); } publicclassInitServiceImplimplementsInitService { "${data.param}") (privateStringdataParam; publicvoidinitData() { System.out.println("initData Service:param is "+dataParam); } }
准备个配置项在properties中
data.param=data param
一、直接在main方法里干,直接果断
publicclassSpringbootApplication { publicstaticvoidmain(String[] args) { ApplicationContextac=SpringApplication.run(Springboot26DemoApplication.class, args); InitServiceinitServiceImpl= (InitService) ac.getBean("initServiceImpl"); if(initServiceImpl!=null){ System.out.println("==== SpringApplication.run Start====="); initServiceImpl.initData(); System.out.println("==== SpringApplication.run End====="); }else{ System.out.println("==== SpringApplication.run Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("==== SpringApplication.run End====="); } } }
二、@PostConstruct注解
publicclassSpringbootApplication { publicstaticvoidmain(String[] args) { ApplicationContextac=SpringApplication.run(Springboot26DemoApplication.class, args); } privateInitServiceinitServiceImpl; publicvoidinit(){ if(initServiceImpl!=null){ System.out.println("====PostConstruct Start====="); initServiceImpl.initData(); System.out.println("====PostConstruct End====="); }else{ System.out.println("====PostConstruct Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("====PostConstruct End====="); } } }
三、InitializingBean接口
publicclassInitBeanInitimplementsInitializingBean { privateInitServiceinitServiceImpl; publicvoidafterPropertiesSet() throwsException { if(initServiceImpl!=null){ System.out.println("====InitializingBean afterPropertiesSet Start====="); initServiceImpl.initData(); System.out.println("====InitializingBean afterPropertiesSet End====="); }else{ System.out.println("====InitializingBean afterPropertiesSet Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("====InitializingBean afterPropertiesSet End====="); } } }
四、@Bean(initMethod = "init")注解
publicclassInitDataBean { privateInitServiceinitServiceImpl; initMethod="init") (publicvoidinit() { if(initServiceImpl!=null){ System.out.println("====@Bean(initMethod = \"init\") Start====="); initServiceImpl.initData(); System.out.println("====@Bean(initMethod = \"init\") End====="); }else{ System.out.println("====@Bean(initMethod = \"init\") Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("====@Bean(initMethod = \"init\") End====="); } } }
五、ApplicationListener<ApplicationReadyEvent>接口
publicclassInitAppListenerimplementsApplicationListener<ApplicationReadyEvent> { privateInitServiceinitServiceImpl; publicvoidonApplicationEvent(ApplicationReadyEventevent) { if(initServiceImpl!=null){ System.out.println("====ApplicationReadyEvent Start====="); initServiceImpl.initData(); System.out.println("====ApplicationReadyEvent End====="); }else{ System.out.println("====ApplicationReadyEvent Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("====ApplicationReadyEvent End====="); } } }
六、ApplicationRunner接口
publicclassInitAppRunnerimplementsApplicationRunner { privateInitServiceinitServiceImpl; publicvoidrun(ApplicationArgumentsargs) throwsException { if(initServiceImpl!=null){ System.out.println("====ApplicationRunner Start====="); initServiceImpl.initData(); System.out.println("====ApplicationRunner End====="); }else{ System.out.println("====ApplicationRunner Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("====ApplicationRunner End====="); } } }
七、CommandLineRunner接口
publicclassInitCommandRunnerimplementsCommandLineRunner { privateInitServiceinitServiceImpl; publicvoidrun(String... args) throwsException { if(initServiceImpl!=null){ System.out.println("====CommandLineRunner Start====="); initServiceImpl.initData(); System.out.println("====CommandLineRunner End====="); }else{ System.out.println("====CommandLineRunner Start====="); System.out.println("====initServiceImpl is null====="); System.out.println("====CommandLineRunner End====="); } } }
输出结果如下:
====PostConstruct Start===== initData Service:param is data param ====PostConstruct End===== ====InitializingBean afterPropertiesSet Start===== initData Service:param is data param ====InitializingBean afterPropertiesSet End===== ====@Bean(initMethod = "init") Start===== initData Service:param is data param ====@Bean(initMethod = "init") End===== ====ApplicationRunner Start===== initData Service:param is data param ====ApplicationRunner End===== ====CommandLineRunner Start===== initData Service:param is data param ====CommandLineRunner End===== ====ApplicationReadyEvent Start===== initData Service:param is data param ====ApplicationReadyEvent End===== ==== SpringApplication.run Start===== initData Service:param is data param ==== SpringApplication.run End=====