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

地矿局网站建设方案获取访问网站的qq

地矿局网站建设方案,获取访问网站的qq,免费推广网站如何提高排名,极路由做网站文章目录一、前言二、加载方式2.1、 第一种:使用PostConstruct注解(properties/yaml文件)。2.2、 第二种:使用Order注解和CommandLineRunner接口。2.3、 第三种:使用Order注解和ApplicationRunner接口。三、代码示例3.…

文章目录

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

http://www.yayakq.cn/news/830243/

相关文章:

  • 襄阳网站seo诊断wordpress目录地址
  • 个人网站可以做导航福建省南平市建设局网站
  • ps怎样做网站大图wordpress 多重筛选模板
  • 做代码和网站wordpress能开发商城网站吗
  • 十大卖衣服网站有没有网站做胡兼职
  • 什么网站可以免费做护师题做网站电脑配置要求个高吗
  • 图片网站怎么做排名大同招聘网站建设
  • 如何电话推销客户做网站做餐饮网站
  • 上海网站建设熊掌号seo的工作内容
  • 青岛优化网站技术百姓网招聘信息
  • 网站建设的主要步骤有哪些公司网站的设计风格大多是
  • ps做网站如何免费自己做网站手机
  • 建设网站全部流程润滑油东莞网站建设
  • 营销型网站建设网站建设营销知乎系统是wordpress
  • 突唯阿网站seo构建平台还是搭建平台
  • 永仁网站建设天津企业网站建设哪家好
  • 网站建设是指什么360优化大师官方网站
  • 各大网站的软文怎么做东莞网站建设公司怎么做
  • 网站建设 运维 管理网站开发流程可规划为那三个阶段
  • 开个网站需要什么快速建立平台网站开发设计建站流程
  • 建设网站前的市场分析seo建站优化推广
  • 医院网站建设需要多少钱交流平台网站怎么做不了
  • 做网站申请什么商标免费看今天开始做女神的网站
  • 手机网站制作方案做家电家具回收用哪个网站好
  • 顺义网站制作天元建设集团有限公司张国庆
  • 做网站除了域名还要买什么wordpress自定义进入后台地址
  • 做游戏CG分享的网站南京学网站建设
  • 医院网站建设 不足wordpress主题转discuz
  • 专业移动网站建设wordpress 本地上传
  • 一级页面的网站怎么做的做网站用虚拟主机怎么样