SpringBoot读取配置文件
前言
配置文件一般存放一些系统变量或用户变量,例如数据库数据源的配置。它可以实现在不改变程序源代码的情况下修改程序的变量的值。通过配置文件可以使程序开发变得更加灵活。接下来我将介绍几种常见的在 SpringBoot 中获取配置文件的方式。
我的示例配置文件(userinfo.yml)位置如下:
1 | my-profile: |
通过 @value
读取简单信息
通过在变量前加上注解 @value("${xxx}")
可以将配置信息注入到变量中
1 | package com.pushihao.controller; |
注意:
@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;
public class YmlConfigFactory extends DefaultPropertySourceFactory {
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 | package com.pushihao.controller; |
通过 @ConfigurationProperties
读取配置文件
@ConfigurationProperties 注解可以将配置文件映射成一个类,而配置文件中的每个键就对应类中的每个属性
映射类代码如下:
1 | package com.pushihao.bean; |
使用:
1 | package com.pushihao.controller; |
其他
配置文件优先级
位置决定优先级:
Config data files are considered in the following order:
- Application properties packaged inside your jar ( and YAML variants).
application.properties
- Profile-specific application properties packaged inside your jar ( and YAML variants).
application-{profile}.properties
- Application properties outside of your packaged jar ( and YAML variants).
application.properties
- Profile-specific application properties outside of your packaged jar ( and YAML variants).
application-{profile}.properties
文件后缀名决定优先级:
yml 读取优先级 > properties 读取优先级
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 披萨盒的赛博日志!
评论