文章目录

  • 一、前言
  • 二、加载方式
    • 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 声明的权重值来决定调用的顺序(数字越小,优先级越高)。

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

  1. 【Arduino串口数据保存到excel中常用三种方法】

    [Arduino串口数据保存到excel中常用三种方法] 1. 前言 2. 利用excel自带Data Streamer读取 2.1 启用 Data Streamer 加载项 2.2 刷写代码并将微控 ...

  2. 怎么在html中加入pjax,pjax加载多说的三种方法

    多说官方提供的方法:http://dev.duoshuo.com/docs/50b344447f32d30066000147 这里不进行说明,请进入上述链接进行查看. 效果说明:进入文章页需要点击按钮 ...

  3. VC6.0加载lib文件的三种方法

    MFC编写程序,都要用到动态链接库,MFC相关的动态库有MFCD42和MFC42等,MFC框架程序已经自动加载,那么如何引入第三方的动态链接库到工程中呢? 静态链接库是要先把程序中所需要使用的函数编译 ...

  4. Springboot项目启动时加载数据库数据到内存

    使用@PostConstruct注解 @Component public class CodeCache {public static Map<String, String> codeMa ...

  5. Android--使用LayoutInflater加载布局文件的三种方法

    LayoutInflater的作用类似于 findViewById(), 不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化! 而 findViewById( ...

  6. Java 反射将配置文件数据加载到对象属性中

    Java 反射将配置文件数据加载到对象属性中 Java 反射 可以根据类名找到相应的类,也可以将配置文件中的值加载到对应属性中. 需要用到的包:spring-core-3.1.2.Release.ja ...

  7. SpringBoot项目静态图片加载浏览器不显示问题解决方案

    SpringBoot项目静态图片加载浏览器不显示问题解决方案 项目结构如下: 我是通过Maven创建的以Thymeleaf为模板引擎创建的SpringBoot Web项目,发现加载的图片在浏览器不显示 ...

  8. Java动态加载一个类的几种方法以及invoke

    一.加载一个类的几种方法 接口 IUser package org.me.javaapp;/**** @author Administrator*/ public interface IUser {} ...

  9. 动态加载JS脚本的4种方法

    动态加载JS脚本的4种方法 2006-12-04 15:33 要实现动态加载JS脚本有4种方法: 1.直接document.write <script language="javasc ...

最新文章

  1. java查询mysql装载bean_jsp与javabean链接mysql数据库并查询数据表的简单实例源码
  2. Java 中的 XML:Java 文档模型的用法
  3. 分子生物学之蛋白质与氨基酸
  4. Codeforces 848C. Goodbye Souvenir
  5. linux5种IO模型以及同步异步,阻塞非阻塞
  6. webpack学习笔记1
  7. Unity3d读取XML配置文件
  8. SpringClound介绍
  9. STM32 SPI详解
  10. 我跟 17 位顶级游戏策划人学到的 3 个产品方法论
  11. 火眼公司发布公开漏洞奖励计划
  12. java设计平台是什么_Java程序设计平台
  13. Django ImportError: Settings cannot be imported
  14. oracle安装失败 主机名_PeopleTool 8.58.04 安装
  15. java数据同步视频,Oracle从入门到精通 oracle数据库 全程同步视频教学 内含Oracle Java项目实战教...
  16. unity2019汉化
  17. js 内置对象常用方法
  18. 数据学习(十)-假设检验
  19. Unity Remote5 使用
  20. 视频怎么转文字?快把这些方法收好

热门文章

  1. 微信朋友圈怎么发文字?微信怎么发朋友圈只发文字?
  2. python之re模块详解
  3. STM-32:SPI通信协议/W25Q64简介—软件SPI读写W25Q64
  4. Ubuntu14.04下安装vim显示没有可用的软件包vim-gtk
  5. STP特性(Cisco)
  6. C语言数据结构——环形队列
  7. linux命令行显示主机名和当前目录
  8. Linux命令之nslookup
  9. android手机华为p10,华为P10的手机系统是什么
  10. #《算法竞赛入门经典》勘误