最近在做Spring项目时,使用到了@Value注解,可以动态注入到注解了@Value的属性上;一般我们会把用到的属性(key/value值)统一放到一个或者多个properties文件中,Spring启动时加载properties把属性读到Spring Property组件中,后续的注入交由组件完成。
我们一贯的做法是在application.xml 中,加载PropertiesLoaderSupport组件;
1 |
|
1. 结构
由于PropertyPlaceholderConfigurer
实现了BeanFactoryPostProcessor
接口;BeanFactory
在加载的时候会调用该接口;
那我们看看加载过程;
首先来看看类结构:
1 | ackage org.springframework.beans.factory.config; |
看看postProcessBeanFactory
在BeanFactory初始化时在哪里调用的,我会先从哪个地方调用了这个方法,一步一步找到,最初加载调用的地方;
除了第二行,其它显示的都是注释;鼠标点击第二行进入;最终会跳到:
这个是一个静态的私用方法,在找(由于代码太长我就不粘出来了;大家可以去找找看)PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
方法调用了本类中的静态方法PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
;
那我们就找哪里调用了PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
方法;
发现跳到AbstractApplicationContext
类中;而AbstractApplicationContext
是所有实现ApplicationContext
子类的抽象父类;
我们在看看那个地方调用了AbstractApplicationContext.invokeBeanFactoryPostProcessors
;
最终找到了加载properties的源头,refresh()方法我就过多介绍了;
2. 如何加载Properties
上面我们知道类的加载过程,接下来看看properties如何加载的。
回到PlaceholderConfigurerSupport.postProcessBeanFactory()
1 |
|
- mergeProperties (): 改方法主要做了两件事
- 加载properties文件 loadProperties()
- 合并指定的Property (参考原码)
- convertProperties(): 该方法没有做什么处理,但是有兴趣的同学可以看看源码,去了解一下;
- processProperties():这是个抽象方法,交由子类实现;
子类就是文件开头介绍的PropertyPlaceholderConfigurer,看看该类实现了什么功能。1
2
3
4
5
6
7
8
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
this.doProcessProperties(beanFactoryToProcess, valueResolver);
}
Spring 最终把处理${} 或者 @Value的方式交由StringValueResolver来完成;
源码介绍的不够全面,需要你进入源码中,一步一步去理解。
3. 如何解析Properties
通过看源码发现最终属性解析是在PropertyPlaceholderConfigurer.resolvePlaceholder
方法中。
1 | protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) { |
参数介绍
- placeholder:要解析的属性key
- props: PlaceholderConfigurerSupport.postProcessBeanFactory()方法中解析的properties
- systemPropertiesMode:有三个值
- SYSTEM_PROPERTIES_MODE_NEVER:不从
System.getProperty()
中获取placeholder
对应的值。 - SYSTEM_PROPERTIES_MODE_FALLBACK: 如果
props
中找不到placeholder
对应的值,就从System.getProperty()
或者System.getenv()
中找。 - SYSTEM_PROPERTIES_MODE_OVERRIDE: 优先从
System.getProperty()
或者System.getenv()
中找placeholder
对应的值,如果找不到,才会从props
中找- System.getProperty(): 如果
systemPropertiesMode
的值为SYSTEM_PROPERTIES_MODE_FALLBACK
和SYSTEM_PROPERTIES_MODE_OVERRIDE
时,默认是从System.getProperty()
中查找的; - System.getenv(): 如果
System.getProperty()
中没有有效的值,才会到System.getenv()
中查找;但是要从System.getenv()
中查找是受PropertyPlaceholderConfigurer.searchSystemEnvironment
限制。
- System.getProperty(): 如果
- SYSTEM_PROPERTIES_MODE_NEVER:不从
写到最后我们总结一下PropertyPlaceholderConfigurer
加载
properties
文件localProperties
这个要指定合并的Properties
解析
props
System.getProperty()
System.getenv()
Spring 版本 4+
具体实现参考源码。
- PropertyPlaceholderConfigurer
- BeanFactoryPostProcessor
- AbstractApplicationContext
- PostProcessorRegistrationDelegate
- PlaceholderResolvingStringValueResolver
- PropertyPlaceholderConfigurerResolver
- SpringProperties
- spring.properties
最后可以看看PropertySourcesPlaceholderConfigurer
和PropertyPlaceholderConfigurer
的实现方式有什么不一样?