惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
T
Troy Hunt's Blog
L
Lohrmann on Cybersecurity
S
Schneier on Security
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
GRAHAM CLULEY
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
SecWiki News
SecWiki News
博客园 - 叶小钗
博客园 - Franky
V
Vulnerabilities – Threatpost
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
O
OpenAI News
小众软件
小众软件
V
V2EX
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
Project Zero
Project Zero
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
Google Online Security Blog
Google Online Security Blog
T
Tailwind CSS Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
量子位
D
Docker
Recent Announcements
Recent Announcements
T
Threat Research - Cisco Blogs
P
Privacy International News Feed
爱范儿
爱范儿
PCI Perspectives
PCI Perspectives
Jina AI
Jina AI
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
大猫的无限游戏
大猫的无限游戏
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
Cloudbric
Cloudbric
S
Security Affairs
F
Fortinet All Blogs

披萨盒的赛博日志

谈谈工作了半年的感受 使用策略模式重构复杂业务分支 像 systemd 一样管理 MacOS 后台常驻任务 以ORM看封装的边界 Git Merge VS Git Rebase: 如何优雅地合并分支? 修改Linux内核模块以支持WG OpenLDAP折腾日记 非特权模式容器 ssh 登录问题 在 Linux 开发环境中使用网络代理 白嫖 Aseprite 像素绘图软件 MongoDB 增删改查 Python数据分析工具包-Numpy 解决 CLion 中文乱码问题 搭建 RLCraft 服务器 Centos 配置 LNMP 环境 部署项目时遇到的坑 浅谈 xhr 请求跨域问题 JavaScript 学习笔记 Eclipse配置Web开发环境 Vue2 基本知识 Ribbon 简单使用 Nacos 简单使用 Spring Cloud Alibaba 环境搭建 什么是RSS?什么是Feed?它们有什么关系? Docker基本使用 TensorFlow启用GPU加速 如何进行内网穿透 Hello World! Git基本使用 Butterfly常用标签外挂
SpringBoot读取配置文件
披萨盒 · 2022-09-30 · via 披萨盒的赛博日志

前言

配置文件一般存放一些系统变量或用户变量,例如数据库数据源的配置。它可以实现在不改变程序源代码的情况下修改程序的变量的值。通过配置文件可以使程序开发变得更加灵活。接下来我将介绍几种常见的在 SpringBoot 中获取配置文件的方式。

我的示例配置文件(userinfo.yml)位置如下:

image-20220930203751776

1
2
3
4
5
6
7
8
9
10
11
12
my-profile:
name: grape
age: 6


users:
- name: 张三
age: 20
- name: 李四
age: 21
- name: 王五
age: 22

通过 @value 读取简单信息

通过在变量前加上注解 @value("${xxx}") 可以将配置信息注入到变量中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.pushihao.controller;

import com.pushihao.bean.YmlConfigFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@PropertySource(value = {"classpath:userinfo.yml"},encoding="UTF-8",factory = YmlConfigFactory.class)
public class UserinfoController {
@Value("${my-profile.name}")
private String name;

@Value("${my-profile.age}")
private String age;


@RequestMapping("/u1")
public String u1() {
return "name:" + name + "|age:" + age;
}
}

注意:

@PropertySource 注解可以指定要读取的配置文件的位置。不写此注解默认读取SpringBoot默认配置文件application.properties/application.yml/application.yaml。

因为 @PropertySource 注解默认是读取 properties 文件,所以如果是读取 properties 文件,注解可以写成 @PropertySource(value = {“classpath:userinfo.properties”},encoding=”UTF-8”)。

本例中读取的是 yml 文件,需要重写 DefaultPropertySourceFactory,让其加载 yml 文件。然后在 PropertySource 注解中加入factory = YmlConfigFactory.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.pushihao.bean;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Properties;

@Component
public class YmlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException, IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
assert sourceName != null;
return new PropertiesPropertySource(sourceName, new Properties());
} else {
assert sourceName != null;
if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
}

private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}

通过 Environment 读取配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.pushihao.controller;

import com.pushihao.bean.YmlConfigFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@PropertySource(value = {"classpath:userinfo.yml"},encoding="UTF-8",factory = YmlConfigFactory.class)
public class Userinfo2Controller {

@Autowired
private Environment environment;

@RequestMapping("/u2")
public String u2() {
String name = environment.getProperty("my-profile.name");
String age = environment.getProperty("my-profile.age");
return "name:" + name + "|age:" + age;
}
}

通过 @ConfigurationProperties 读取配置文件

@ConfigurationProperties 注解可以将配置文件映射成一个类,而配置文件中的每个键就对应类中的每个属性

映射类代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.pushihao.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Data
@PropertySource(value = {"classpath:userinfo.yml"},encoding="UTF-8",factory = YmlConfigFactory.class)

@ConfigurationProperties(prefix = "")
public class UserinfoProperties {


private User myProfile = new User();

private List<User> users = new ArrayList<>();


@Data
public static class User {
String name;

Integer age;
}
}

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.pushihao.controller;

import com.pushihao.bean.UserinfoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Userinfo3Controller {
@Autowired
private UserinfoProperties userinfoProperties;

@RequestMapping("/u3")
public String u3() {
return "name:" + userinfoProperties.getMyProfile().getName()
+ "|age:" + userinfoProperties.getMyProfile().getAge()
+ "|users:" + userinfoProperties.getUsers().toString();
}
}

其他

配置文件优先级

位置决定优先级:

Config data files are considered in the following order:

  1. Application properties packaged inside your jar ( and YAML variants).application.properties
  2. Profile-specific application properties packaged inside your jar ( and YAML variants).application-{profile}.properties
  3. Application properties outside of your packaged jar ( and YAML variants).application.properties
  4. Profile-specific application properties outside of your packaged jar ( and YAML variants).application-{profile}.properties

文件后缀名决定优先级:

yml 读取优先级 > properties 读取优先级

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 披萨盒的赛博日志


avatar

披萨盒

反正身体这么好,今天继续笑下去吧

个人主页