原文来自个人小站 ( Ariescat’s Blog ) :Spring之properties解析
Spring Properties使用的几种方式
-
在Java中使用这个 @Value("${ }")注解 读取
-
在配置文件中使用 ${ } 读取
解析器注册
-
context:property-placeholder
标签<context:property-placeholder location="classpath:*.properties"/>
源码解析:
public class ContextNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser()); ...省略代码 } }
这个
Parser
实质上注册了一个org.springframework.context.support.PropertySourcesPlaceholderConfigurer(实现了BeanFactoryPostProcessor
的处理器)。// PropertySourcesPlaceholderConfigurer.java @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.propertySources == null) { this.propertySources = new MutablePropertySources(); if (this.environment != null) { this.propertySources.addLast( // 注意看这里this.environment里包含了propertySources // spring boot的配置都在这里 new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) { @Override @Nullable