SpringBoot项目启动数据加载内存的三种方法

 更新时间:2024年04月30日 09:13:44   作者:No8g攻城狮  
一般来说,SpringBoot工程环境配置放在properties文件中,启动的时候将工程中的properties/yaml文件的配置项加载到内存中,本文给大家介绍了SpringBoot项目启动数据加载内存中的三种方法,需要的朋友可以参考下

一、前言

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

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

二、加载方式

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

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

第三种:使用@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>();
 
    @Autowired
    private ICodeService codeService;
 
    @PostConstruct
    public 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));
        }
    }
 
    @PreDestroy
    public 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>();
 
    @Autowired
    private ICodeService codeService;
 
    @Override
    public 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>();
 
    @Autowired
    private ICodeService codeService;
 
    @Override
    public 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));
        }
    }
}

四、总结

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

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

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

以上就是SpringBoot项目启动数据加载内存中的三种方法的详细内容,更多关于SpringBoot数据加载内存的资料请关注脚本之家其它相关文章!

相关文章

  • java虚拟机学习笔记基础篇

    java虚拟机学习笔记基础篇

    在本篇文章里小编给大家整理的是关于java虚拟机学习笔记的相关内容,分享了一些基础知识点,需要的朋友们参考下。
    2019-06-06
  • Java Idea TranslationPlugin翻译插件使用解析

    Java Idea TranslationPlugin翻译插件使用解析

    这篇文章主要介绍了Java Idea TranslationPlugin翻译插件使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 第三方包jintellitype实现Java设置全局热键

    第三方包jintellitype实现Java设置全局热键

    本文主要介绍了,在java中使用第三方插件包jintellitype来实现全局热键,非常的简单,但是很实用,有需要的朋友可以参考下,欢迎一起来参与改进此项目
    2014-09-09
  • spring中ioc是什么

    spring中ioc是什么

    IoC是一种让服务消费者不直接依赖于服务提供者的组件设计方式,是一种减少类与类之间依赖的设计原则。下面通过本文给大家分享spring中ioc的概念,感兴趣的朋友一起看看吧
    2017-09-09
  • java读取txt文件并输出结果

    java读取txt文件并输出结果

    这篇文章主要介绍了java读取txt文件并输出结果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 详解Spring Boot自动装配的方法步骤

    详解Spring Boot自动装配的方法步骤

    这篇文章主要介绍了详解Spring Boot自动装配的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • 从内存模型中了解Java final的全部细节

    从内存模型中了解Java final的全部细节

    关于final关键字,它也是我们一个经常用的关键字,可以修饰在类上、或者修饰在变量、方法上,以此看来定义它的一些不可变性!像我们经常使用的String类中,它便是final来修饰的类,并且它的字符数组也是被final所修饰的。但是一些final的一些细节你真的了解过吗
    2022-03-03
  • SpringBoot集成Redis实现消息队列的方法

    SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • SpringMVC4.3解析器HandlerMethodArgumentResolver接口源码

    SpringMVC4.3解析器HandlerMethodArgumentResolver接口源码

    这篇文章主要为大家介绍了SpringMVC4.3解析器HandlerMethodArgumentResolver接口源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Java数字图像处理之图像灰度处理

    Java数字图像处理之图像灰度处理

    这篇文章主要为大家详细介绍了Java数字图像处理之图像灰度处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06

最新评论