第7章 Spring的注解@PropertySource 使用与深入理解

本文详细介绍了Spring的@PropertySource注解,包括如何使用它来加载配置文件,解析带有占位符的键值对,以及在多个文件中如何覆盖配置。同时,文章通过源码分析了@PropertySource处理过程,涉及到PropertyPlaceholderHelper的占位符解析,并给出了调用时序图和类图,帮助读者深入理解其工作原理。最后,讨论了PropertySource和PropertySources的数据结构及其在Environment中的查找顺序。

@PropertySource 注解用来增加 PropertySource 到 Spring 的 Environment 中。和 @Configuration 注解结合使用。

使用例子

给一个指定文件 app.properties 包含一个键值对 bean.name=java.lang.String@PropertySource 会把 app.properties 放入到 Environment 里。

@Configuration
@PropertySource("classpath:/app.properties")
public class AccountConfig {

	@Autowired
	Environment env;

	@Bean
	public People people() {
		System.out.println("===================");
		String[] defaultProfiles = env.getDefaultProfiles();
		Arrays.stream(defaultProfiles).forEach(System.out::println);
		System.out.println(env.getProperty("bean.name"));
		return new People();
	}
}
解析带有占位符
@Configuration
@PropertySource("classpath:/${my.placheholder:app}.properties")
public class AccountConfig {

	@Autowired
	Environment env;

	@Bean
	public People people() {
		System.out.println("===================");
		String[] defaultProfiles = env.getDefaultProfiles();
		Arrays.stream(defaultProfiles).forEach(System.out::println);
		System.out.println(env.getProperty("bean.name"));
		return new People();
	}
}

如果占位符 my.placheholder 已经注册到 Environment ,那么占位符将被解析成对应的值。如果没有找到对应值,那么将会使用默认值 app

覆盖

如果有多个 .property 的文件,并且每个文件都包含相同的键名,那么存在覆写。

 @Configuration
 @PropertySource("classpath:/com/myco/a.properties")
 public class ConfigA { }

 @Configuration
 @PropertySource("classpath:/com/myco/b.properties")
 public class ConfigB { }
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 ctx.register(ConfigA.class);
 ctx.register(ConfigB.class);
 ctx.refresh();
源码分析
处理@PropertSource注解

@PropertySource 注解和 @Configuration 注解结合使用,那源码的入口就是 ConfigurationClassPostProcessor#processConfigBeanDefinitions,在方法内创建了类ConfigurationClassParser 。由 ConfigurationClassParser#processPropertySource 方法负责解析处理 @PropertySource。大概逻辑就是:获取资源路径,然后由 Environment 解析路径占位符,得到实际资源路径,然后由 ResourceLoader 根据路径加载资源 Resource,最后把 Resource 转成 PropertySource 放入 Environment 里。

private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
    	// 获取name,一个name对应一个PropertySource
		String name = propertySource.getString("name");
		if (!StringUtils.hasLength(name)) {
			name = null;
		}
    	// 指定编码类型
		String encoding = propertySource.getString("encoding");
		if (!StringUtils.hasLength(encoding)) {
			encoding = null;
		}
    	// 获取资源路径
		String[] locations = propertySource.getStringArray("value");
		Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
		boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");

		Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
		PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
				DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));

		for (String location : locations) {
			try {
                // 解析路径占位符
				String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
                // 加载资源
				Resource resource = this.resourceLoader.getResource(resolvedLocation);
                // 把Resource转成PropertySource,并放入Environment里
				addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
			}
			catch (IllegalArgumentException | FileNotFoundException | UnknownHostException | SocketException ex) {
				// 省略异常处理
			}
		}
	}
解析占位符 PropertyPlaceholderHelper

PropertyPlaceholderHelper 来负责解析替换占位符。具体源码不分析,来看一下具体使用几个例子。

public class PropertyPlaceholderHelperTests {
    private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
    
    @Test
	void withProperties() {
		String text = "foo=${foo}";
		Properties props = new Properties();
		props.setProperty("foo", "bar");

		assertThat(this.helper.replacePlaceholders(text, props)).isEqualTo("foo=bar");
	}

	@Test
	void withMultipleProperties() {
		String text = "foo=${foo},bar=${bar}";
		Properties props = new Properties();
		props.setProperty("foo", "bar");
		props.setProperty("bar", "baz");

		assertThat(this.helper.replacePlaceholders(text, props)).isEqualTo("foo=bar,bar=baz");
	}

	@Test
	void recurseInProperty() {
		String text = "foo=${bar}";
		Properties props = new Properties();
		props.setProperty("bar", "${baz}");
		props.setProperty("baz", "bar");

		assertThat(this.helper.replacePlaceholders(text, props)).isEqualTo("foo=bar");
	}

	@Test
	void recurseInPlaceholder() {
		String text = "foo=${b${inner}}";
		Properties props = new Properties();
		props.setProperty("bar", "bar");
		props.setProperty("inner", "ar");

		assertThat(this.helper.replacePlaceholders(text, props)).isEqualTo("foo=bar");

		text = "${top}";
		props = new Properties();
		props.setProperty("top", "${child}+${child}");
		props.setProperty("child", "${${differentiator}.grandchild}");
		props.setProperty("differentiator", "first");
		props.setProperty("first.grandchild", "actualValue");

		assertThat(this.helper.replacePlaceholders(text, props)).isEqualTo("actualValue+actualValue");
	}

	@Test
	void withResolver() {
		String text = "foo=${foo}";
		PlaceholderResolver resolver = placeholderName -> "foo".equals(placeholderName) ? "bar" : null;

		assertThat(this.helper.replacePlaceholders(text, resolver)).isEqualTo("foo=bar");
	}

	@Test
	void unresolvedPlaceholderIsIgnored() {
		String text = "foo=${foo},bar=${bar}";
		Properties props = new Properties();
		props.setProperty("foo", "bar");

		assertThat(this.helper.replacePlaceholders(text, props)).isEqualTo("foo=bar,bar=${bar}");
	}

	@Test
	void unresolvedPlaceholderAsError() {
		String text = "foo=${foo},bar=${bar}";
		Properties props = new Properties();
		props.setProperty("foo", "bar");

		PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false);
		assertThatIllegalArgumentException().isThrownBy(() ->
				helper.replacePlaceholders(text, props));
	}
}
总结

PropertySource 就是根据指定资源路径去加载资源,然后把加载的 Resource 放入到 Environment 中去。

调用时序图

在这里插入图片描述

类图

在这里插入图片描述

扩展

Environment.getProperty(String key):是有顺序的。首先会从 systemProperties 里获取,如果获取不到,再从 systemEnvironment 获取;如果获取不到,则从由 @PropertySource 指定路径获取。

PropertySource

ProertySource 可以看作是键值对一种数据结构,包含两个字段 name 和 泛型类型 T

public abstract class PropertySource<T> {
    protected String name;
    protected T source;
}
PropertySources

PropertySources 可以看作是 PropertySource 的List结构版本,可以存放多个。

public interface PropertySources extends Iterable<PropertySource<?>> {
    boolean contains(String name);
    
    PropertySource<?> get(String name);
}
MutablePropertySources

MutablePropertySources 是实现类。结构如下所示

public class MutablePropertySources implements PropertySources {
    private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<>();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值