SpringBoot引入外部配置文件

前言

配置预习知识点: PropertyPlaceholderConfigurer, PropertySourcesPlaceholderConfigurer, EnvironmentPostProcessor

@PropertySource引入外部配置文件

自定义配置文件config.properties

1
2
idea360.username=admin
idea360.password=admin

配置bean

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 cn.idea360.prop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
* @author cuishiying
* @date 2021-01-22
*/
@Configuration
@ComponentScan(basePackages = "cn.idea360.prop")
@PropertySource(value = "classpath:config.properties")
public class PropertyConfig {

@Autowired
Environment environment;

public PropertyConfig() {
System.out.println("PropertyConfig init");
}

public javax.sql.DataSource dataSource(){
String user = this.environment.getProperty("idea360.username");
System.out.println("user=" + user); // user=admin
return null;
}
}

测试

1
2
3
4
5
6
@Test
public void test09() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PropertyConfig.class);
PropertyConfig bean = applicationContext.getBean(PropertyConfig.class);
bean.dataSource();
}

EnvironmentPostProcessor封装自定义配置文件

一般应用在封装自定义组件及内置配置文件中

内置配置文件

  1. 添加配置
1
2
3
4
5
6
7
8
9
10
11
12
13
@Component
public class TestEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
// classPath下的yml文件加载
ClassPathResource classPathResource=new ClassPathResource("test.yml");
yaml.setResources(classPathResource);
Properties properties=yaml.getObject();
PropertiesPropertySource propertySource =new PropertiesPropertySource("TestProperties", properties);
environment.getPropertySources().addLast(propertySource);
}
}
  1. resources目录下配置test.yml
1
2
3
easyliao:
name: idea360
password: 123456
  1. META-INF/spring.factories中配置
1
2
org.springframework.boot.env.EnvironmentPostProcessor=\
com.easyliao.framework.config.TestEnvironmentPostProcessor
  1. 项目中使用
1
2
@Value("${easyliao.name}")
private String name;

替换日志配置文件

  1. 替换默认日志格式
1
2
3
4
5
6
7
8
9
10
11
12
public class TraceEnvironmentPostProcessor implements EnvironmentPostProcessor {

private static final String PROPERTY_SOURCE_NAME = "defaultProperties";
private static final String LEVEL_STR_PARENT = "%5p [%X{traceId:-}]";

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> map = new HashMap<>(1);
map.put("logging.pattern.level", LEVEL_STR_PARENT);
EnvironmentUtils.addOrReplace(environment.getPropertySources(), map, PROPERTY_SOURCE_NAME);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class EnvironmentUtils {
public static void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map, String propertySourceName) {
MapPropertySource target = null;
if (propertySources.contains(propertySourceName)) {
PropertySource<?> source = propertySources.get(propertySourceName);
if (source instanceof MapPropertySource) {
target = (MapPropertySource) source;
for (String key : map.keySet()) {
if (!target.containsProperty(key)) {
target.getSource().put(key, map.get(key));
}
}
}
}
if (target == null) {
target = new MapPropertySource(propertySourceName, map);
}
if (!propertySources.contains(propertySourceName)) {
propertySources.addLast(target);
}
}
}
  1. META-INF/spring.factories中配置
1
2
org.springframework.boot.env.EnvironmentPostProcessor=\
com.easyliao.framework.log.processor.TraceEnvironmentPostProcessor

最后

本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力