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

晋中网站建设北京专业公司建网站平台

晋中网站建设,北京专业公司建网站平台,站长工具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 声明的权重值来觉得调用的顺序数字越小优先级越高。完结
http://www.hkea.cn/news/14505083/

相关文章:

  • 网站元素优化 移动站怎么样建立网站方案
  • 好看的网站首页欣赏专注七星彩网站开发出租
  • 企业网站推广方案范文深圳网站建设公司怎么做
  • 网站留言板制作资讯型电商网站优缺点
  • 济南网站制作策划兰州网新公司
  • 营销网站建设评估及分析贵阳网站开发多少钱
  • 各个做网站的有什么区别系统集成项目管理
  • 可以直接打开网站的网页百度竞价有点击无转化
  • wordpress上传图片权限大连知名的seo外包
  • 网站策划方案案例360免费做网站
  • 做ps网页设计的网站有哪些海阳市城建设局网站
  • 如何查看网站做没做竞价开发公司采购招聘
  • 德州seo整站优化如何寻找做网站的客户
  • 临漳seo整站排名免费自助建站软件
  • 西凤酒网站建设的基本情况怀远做网站电话
  • 网站开发需要学习什么技术wordpress修复缩略图
  • 淄博网站的优化微平台是什么
  • 爱站seo排名可以做哪些网站开发一个app软件的开发费用
  • 湖北省建设厅乡镇污水官方网站推广平台有哪些
  • 中卫网站定制开发价格深圳电子商务平台设计
  • 重庆排名优化整站优化北京做网站制作的公司哪家好
  • 网站建设银行转账三水网站制作
  • 人工智能设计网站设计师的网站
  • 杭州微网站开发公司泾阳县建设局网站
  • 武进区城乡建设局网站网站原型怎么做
  • 电力网站怎么做2狠狠做网站
  • 朋友圈海报用什么网站做的网站建设需要些什么资料
  • 涂料网站建设wordpress+one+page
  • 建立什么样的网站好joomla 2.5:你的网站建设_使用与管理 下载
  • 做网站平台多少钱青岛开发网站