0
点赞
收藏
分享

微信扫一扫

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

水沐由之 2022-02-19 阅读 83

@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));
	}
}
调用时序图

在这里插入图片描述

类图

在这里插入图片描述

扩展

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<>();
}
举报

相关推荐

0 条评论