晋中网站建设,北京专业公司建网站平台,站长工具a级,网站开发需要什么基础只是一、前言一般来说#xff0c;SpringBoot工程环境配置放在properties文件中#xff0c;启动的时候将工程中的properties/yaml文件的配置项加载到内存中。但这种方式改配置项的时候#xff0c;需要重新编译部署#xff0c;考虑到这种因素#xff0c;今天介绍将配置项存到数据…一、前言一般来说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 MapInteger, String codeMap new HashMapInteger, String();Autowiredprivate ICodeService codeService;PostConstructpublic void init() {System.out.println(示例1加载codeMap中......);// 查询数据库数据ListString 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 MapInteger, String codeMap new HashMapInteger, String();Autowiredprivate ICodeService codeService;Overridepublic void run(String... args) throws Exception {System.out.println(示例2加载codeMap中......);// 查询数据库数据ListString 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 MapInteger, String codeMap new HashMapInteger, String();Autowiredprivate ICodeService codeService;Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println(示例3加载codeMap中......);// 查询数据库数据ListString 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 声明的权重值来觉得调用的顺序数字越小优先级越高。完结