SpringBoot项目启动过程中做数据资源初始化的方式

简介: SpringBoot项目启动过程中做数据资源初始化的方式

SpringBoot项目经常遇到启动项目的过程中需要初始化一些数据,比如字典,前置条件数据之类的需求,这个时候需要在项目启动的过程中去做这一块的工作。下面根据经验总结了以下几种项目启动的时候初始化数据的方法,可以选择一个自己喜欢的,从项目启动运行可以看出来他们的加载顺序,在这里不对加载顺序做解释。

以下代码运行在jdk17,SpringBoot3.0.2环境下

先准备下面的接口和服务类,做三层业务逻辑开发的同学都懂

publicinterfaceInitService {
voidinitData();
}
@ServicepublicclassInitServiceImplimplementsInitService {
@Value("${data.param}")
privateStringdataParam;
@OverridepublicvoidinitData() {
System.out.println("initData Service:param is "+dataParam);
    }
}

准备个配置项在properties中

data.param=data param

一、直接在main方法里干,直接果断

@SpringBootApplicationpublicclassSpringbootApplication {
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注解

@SpringBootApplicationpublicclassSpringbootApplication {
publicstaticvoidmain(String[] args) {
ApplicationContextac=SpringApplication.run(Springboot26DemoApplication.class, args);
    }
@AutowiredprivateInitServiceinitServiceImpl;
@PostConstructpublicvoidinit(){
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接口

@ComponentpublicclassInitBeanInitimplementsInitializingBean {
@AutowiredprivateInitServiceinitServiceImpl;
@OverridepublicvoidafterPropertiesSet() 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")注解

@ComponentpublicclassInitDataBean {
@AutowiredprivateInitServiceinitServiceImpl;
@Bean(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>接口

@ComponentpublicclassInitAppListenerimplementsApplicationListener<ApplicationReadyEvent> {
@AutowiredprivateInitServiceinitServiceImpl;
@OverridepublicvoidonApplicationEvent(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接口

@ComponentpublicclassInitAppRunnerimplementsApplicationRunner {
@AutowiredprivateInitServiceinitServiceImpl;
@Overridepublicvoidrun(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接口

@ComponentpublicclassInitCommandRunnerimplementsCommandLineRunner {
@AutowiredprivateInitServiceinitServiceImpl;
@Overridepublicvoidrun(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=====


目录
打赏
0
0
0
0
96
分享
相关文章
SpringBoot整合Flowable【06】- 查询历史数据
本文介绍了Flowable工作流引擎中历史数据的查询与管理。首先回顾了流程变量的应用场景及其局限性,引出表单在灵活定制流程中的重要性。接着详细讲解了如何通过Flowable的历史服务API查询用户的历史绩效数据,包括启动流程、执行任务和查询历史记录的具体步骤,并展示了如何将查询结果封装为更易理解的对象返回。最后总结了Flowable提供的丰富API及其灵活性,为后续学习驳回功能做了铺垫。
78 0
SpringBoot整合Flowable【06】- 查询历史数据
SpringBoot项目打war包流程
本文介绍了将Spring Boot项目改造为WAR包并部署到外部Tomcat服务器的步骤。主要内容包括:1) 修改pom.xml中的打包方式为WAR;2) 排除Spring Boot内置的Tomcat依赖;3) 添加Servlet API依赖;4) 改造启动类以支持WAR部署;5) 打包和部署。通过这些步骤,可以轻松地将Spring Boot应用转换为适合外部Tomcat服务器的WAR包。
155 64
SpringBoot项目打war包流程
SpringBoot 通过集成 Flink CDC 来实时追踪 MySql 数据变动
通过详细的步骤和示例代码,您可以在 SpringBoot 项目中成功集成 Flink CDC,并实时追踪 MySQL 数据库的变动。
175 43
SpringBoot项目打包成war包
通过上述步骤,我们成功地将一个Spring Boot应用打包成WAR文件,并部署到外部的Tomcat服务器中。这种方式适用于需要与传统Servlet容器集成的场景。
30 8
|
2月前
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
161 36
SpringBoot start.aliyun.com创建项目,解决properties乱码的问题
通过确保文件和开发环境的编码一致,配置 Maven 编码,设置 Spring Boot 应用和嵌入式服务器的编码,可以有效解决 properties 文件的乱码问题。以上步骤可以帮助开发者确保在 Spring Boot 项目中正确处理和显示多语言字符,避免因编码问题导致的乱码现象。
46 5
SpringBoot整合Flowable【05】- 使用流程变量传递业务数据
本文介绍了如何使用Flowable的流程变量来管理绩效流程中的自定义数据。首先回顾了之前的简单绩效流程,指出现有流程缺乏分数输入和保存步骤。接着详细解释了流程变量的定义、分类(运行时变量和历史变量)及类型。通过具体代码示例展示了如何在绩效流程中插入全局和局部流程变量,实现各节点打分并维护分数的功能。最后总结了流程变量的使用场景及其在实际业务中的灵活性,并承诺将持续更新Flowable系列文章,帮助读者更好地理解和应用Flowable。 简要来说,本文通过实例讲解了如何利用Flowable的流程变量功能优化绩效评估流程,确保每个环节都能记录和更新分数,同时提供了全局和局部变量的对比和使用方法。
104 0
SpringBoot整合Flowable【05】- 使用流程变量传递业务数据
SpringBoot获取项目文件的绝对路径和相对路径
SpringBoot获取项目文件的绝对路径和相对路径
187 1
SpringBoot获取项目文件的绝对路径和相对路径
手写模拟Spring Boot启动过程功能
【11月更文挑战第19天】Spring Boot自推出以来,因其简化了Spring应用的初始搭建和开发过程,迅速成为Java企业级应用开发的首选框架之一。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,帮助读者深入理解其工作机制。
66 3
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型 图像处理 光通信 分布式计算 算法语言 信息技术 计算机应用
82 8