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

足球网站建设网站排名优化客服

足球网站建设,网站排名优化客服,网站流量分析系统,三门峡市住房的城乡建设局网站一、前言一般来说,SpringBoot工程环境配置放在properties文件中,启动的时候将工程中的properties/yaml文件的配置项加载到内存中。但这种方式改配置项的时候,需要重新编译部署,考虑到这种因素,今天介绍将配置项存到数据…

一、前言

一般来说,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>();@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));}}
}

四、总结

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

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

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

完结!

http://www.ds6.com.cn/news/100183.html

相关文章:

  • seo高手培训优化防控措施
  • 如何搭建英文网站b站免费推广app大全
  • 一个完整的网站推广方案优化疫情防控
  • 宁波网站公司网站优化排名技巧
  • 织梦系统做网站域名检测
  • 自助建站代理宁波网络优化seo
  • 成立网站是不是需要先成立公司免费发外链的网站
  • 商城型网站的概念游戏推广员拉人犯法吗
  • 用美图秀秀做网站图片网络营销服务的内容
  • 网站建设的作用品牌营销方案
  • 百中搜如何做网站排名网络推广公司官网
  • 东营网站制作长沙专业做网站公司
  • 海南省建设厅网站首页巢湖网站制作
  • 建筑工程图纸符号大全图解重庆排名优化整站优化
  • 昆明网站建设报价网络营销产品推广方案
  • 朝阳区住房和城乡建设委员会网站aso优化的主要内容
  • 湖南长沙网站建设公司电话东莞搜索seo网站关键词优化
  • 怎么做才能使网站ip增多南京seo网络推广
  • 如何把php做的网站做成appseo中文全称是什么
  • 广安网站建设哪家好百度怎么发布自己的信息
  • 常见的导航网站有哪些品牌营销经典案例
  • windows2008网站西安seo霸屏
  • WordPress图片裁减成都关键词优化报价
  • 人才市场网站建设论文枸橼酸西地那非片的作用及功效
  • 功能性网站开发seo标签优化
  • 什么专业学做网站今天nba新闻最新消息
  • wordpress安装插件无法创建目录360优化大师官方官网
  • 怎么把网站黑了企业seo排名
  • 钦州住房和城乡建设委员会网站seo排名优化首页
  • 网站开发流程属于制作与开发百度seo排名优化费用