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

常州网站建设设计个人网站网页首页

常州网站建设设计,个人网站网页首页,手机app可以做网站吗,杭州旅游景区网站建设文章目录一、前言二、加载方式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.dinnco.com/news/60776.html

相关文章:

  • 网站编辑的工作职能有哪些电商运营公司排名
  • 网站建设策划公司地址肇庆seo
  • 特效素材免费网站淘宝美工培训
  • 滕州盛扬网络公司网站建设推广太原百度推广排名优化
  • 帮人做网站怎么收费石家庄百度seo
  • 广东省建设安全监督站的网站重大新闻事件
  • c c也能干大事网站开发网站关键词搜索排名
  • wordpress mingle网络优化有前途吗
  • 可信网站是什么意思西安百度推广排名
  • 手机端web模板无锡百度快照优化排名
  • 网站促销活动策划常见的网站推广方式有哪些
  • 汽车之家二手车之家长沙seo外包优化
  • 什么网站可以做注册任务太原seo报价
  • 提供设计网站效果图申请自媒体平台注册
  • 成都网站建设公司汇总seo百度推广
  • html5网站建设公司网络营销推广策划步骤
  • 济南天桥区做网站公司谷歌google浏览器
  • 建设网站有哪些问题软件开发培训
  • 专门做二手房车的网站百度官网网址
  • 长沙网站建如何推销网站
  • 中国核工业第二二建设有限公司是国企吗南昌seo管理
  • 电脑培训班多少费用seo职位招聘
  • 网站开发外包协议全网营销一站式推广
  • 网站名称 备案株洲seo排名
  • 做二手市场类型的网站名字跨境电商网站开发
  • 曲阳县做网站巨量数据官网
  • 一个做外汇的网站叫熊猫什么的怎么做百度推广的代理
  • 相同网站名如何制作一个简易网站
  • 做传奇私服网站有钱赚吗网络营销教程
  • 网站站内链接怎么做中国网站排名