当前位置: 首页 > news >正文

【java】springboot项目启动数据加载内存中的三种方法

文章目录

  • 一、前言
  • 二、加载方式
    • 2.1、 第一种:使用@PostConstruct注解(properties/yaml文件)。
    • 2.2、 第二种:使用@Order注解和CommandLineRunner接口。
    • 2.3、 第三种:使用@Order注解和ApplicationRunner接口。
  • 三、代码示例
    • 3.1、 使用@PostConstruct注解
    • 3.2、 CommandLineRunner接口
    • 3.3、 ApplicationRunner接口
  • 四、总结
    • 4.1、CommandLineRunner和ApplicationRunner调用的时机是在容器初始化完成之后,立即调用。
    • 4.2、CommandLineRunner和ApplicationRunner使用上没有区别,唯一区别是CommandLineRunner接受字符串数组参数,需要自行解析出健和值,ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。
    • 4.3、两个接口都可以使用 @Order 参数,支持工程启动后根据order 声明的权重值来决定调用的顺序(数字越小,优先级越高)。

一、前言

一般来说,SpringBoot工程环境配置放在properties文件中,启动的时候将工程中的properties/yaml文件的配置项加载到内存中。但这种方式改配置项的时候,需要重新编译部署,考虑到这种因素,今天介绍将配置项存到数据库表中,在工程启动时把配置项加载到内存中。

SpringBoot提供了两个接口: CommandLineRunner 和 ApplicationRunner 。实现其中接口,就可以在工程启动时将数据库中的数据加载到内存。使用的场景有:加载配置项到内存中;启动时将字典或白名单数据加载到内存(或缓存到Redis中)。

二、加载方式

2.1、 第一种:使用@PostConstruct注解(properties/yaml文件)。

2.2、 第二种:使用@Order注解和CommandLineRunner接口。

2.3、 第三种:使用@Order注解和ApplicationRunner接口。

注意事项

第二种和第三种,二者的官方javadoc一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理。ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。

三、代码示例

3.1、 使用@PostConstruct注解

package com.example.demo.config;import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Component
public class InitData1 {public static Map<Integer, String> codeMap = new HashMap<Integer, String>();@Autowiredprivate ICodeService codeService;@PostConstructpublic void init() {System.out.println("示例1:加载codeMap中......");// 查询数据库数据List<String> codeList = codeService.listAll();for (int i = 0; i < codeList.size(); i++) {codeMap.put(i, codeList.get(i));}}@PreDestroypublic void destroy() {System.out.println("系统启动成功,codeMap加载完成!");}
}

3.2、 CommandLineRunner接口

package com.example.demo.config;import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.List;
import java.util.Map;@Component
@Order(1) // 初始化加载优先级,数字越小优先级越高
public class InitData2 implements CommandLineRunner {public static Map<Integer, String> codeMap = new HashMap<Integer, String>();@Autowiredprivate ICodeService codeService;@Overridepublic void run(String... args) throws Exception {System.out.println("示例2:加载codeMap中......");// 查询数据库数据List<String> codeList = codeService.listAll();for (int i = 0; i < codeList.size(); i++) {codeMap.put(i, codeList.get(i));}}
}

3.3、 ApplicationRunner接口

package com.example.demo.config;import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.List;
import java.util.Map;@Component
@Order(1) // 初始化加载优先级,数字越小优先级越高
public class InitData3 implements ApplicationRunner {public static Map<Integer, String> codeMap = new HashMap<Integer, String>();@Autowiredprivate ICodeService codeService;@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("示例3:加载codeMap中......");// 查询数据库数据List<String> codeList = codeService.listAll();for (int i = 0; i < codeList.size(); i++) {codeMap.put(i, codeList.get(i));}}
}

四、总结

4.1、CommandLineRunner和ApplicationRunner调用的时机是在容器初始化完成之后,立即调用。

4.2、CommandLineRunner和ApplicationRunner使用上没有区别,唯一区别是CommandLineRunner接受字符串数组参数,需要自行解析出健和值,ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。

4.3、两个接口都可以使用 @Order 参数,支持工程启动后根据order 声明的权重值来决定调用的顺序(数字越小,优先级越高)。

http://www.lryc.cn/news/3284.html

相关文章:

  • 【GO】29.go-gin支持ssl/tls,即https示例
  • 逻辑仿真工具VCS的使用-Makefile
  • 信息系统安全技术
  • 【数据结构】最小生成树(Prim算法,普里姆算法,普利姆)、最短路径(Dijkstra算法,迪杰斯特拉算法,单源最短路径)
  • Session与Cookie的区别(一)
  • 【Java】重载和重写的区别
  • AcWing 第 90 场周赛
  • 刚刚,体验了一把Bing chat很爽
  • 牛客网Python篇数据分析习题(二)
  • 如何用Python打包好exe文件,并替换图标
  • NFC概述摘要
  • Python-项目实战--贪吃蛇小游戏(1)
  • vscode sftp从linux服务器下载文件至本地:No such file or dictionary【已解决】
  • 详解指针(2)(初阶版)
  • 超详细讲解字符串查找函数(保姆级教程!!!)
  • LeetCode-1138. 字母板上的路径【哈希表,字符串】
  • Vue 可配置化的路由缓存(Vu2 Vue3)
  • Linux VPU驱动
  • spring 笔记
  • Java日志框架学习
  • 基础面试题:堆和栈的区别
  • (干货教程)在VSCode并使用chatgtp插件编写CC++语言程序
  • 【思维模型】概率思维的价值:找到你的人生算法,实现阶级跃迁!
  • SpringBoot + kotlin/java + Mybatis-Plus +Sqlite + Gradle多模块项目
  • Docker 容器与容器云读书笔记(一)
  • 软件设计(九)
  • FoveaBox原理与代码解析
  • Linux内核启动(1,0.11版本)启动BIOS与加载内核
  • python制作贪吃蛇小游戏,畅玩无限制
  • MySQL-InnoDB数据页结构浅析