context:property-placeholder深入分析

深入分析前,先介绍 BeanFactoryPostProcessor,然后介绍 PropertyPlaceholderConfigurer,最后介绍标签context:property-placeholder。
1、介绍BeanFactoryPostProcessor

参考:http://www.cnblogs.com/sishang/p/6588542.html
Spring中BeanFactoryPostProcessor和BeanPostProcessor都是Spring初始化bean时对外暴露的扩展点。两个接口从名字看起来很相似,但是作用及使用场景却不同。这里主要讲 BeanFactoryPostProcessor

    Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它。这句话不好理解下面举例说明:

首先看一下接口 BeanFactoryPostProcessor
复制代码
    public interface BeanFactoryPostProcessor {  
      
        /** 
         * Modify the application context's internal bean factory after its standard 
         * initialization. All bean definitions will have been loaded, but no beans 
         * will have been instantiated yet. This allows for overriding or adding 
         * properties even to eager-initializing beans. 
         * @param beanFactory the bean factory used by the application context 
         * @throws org.springframework.beans.BeansException in case of errors 
         */  
        void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;  
      
    }  
复制代码



其次看一下spring.xml,这个文件在配置ioc容器
按 Ctrl+C 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 支持Spring注解,先不管这个--> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <!-- 注册一个BeanPostProcessor,先不管这个--> <bean id="postProcessor" class="com.test.spring.PostProcessor"/> <!-- 注册一个BeanFactoryPostProcessor,FactoryPostProcessor继承了接口BeanFactoryPostProcessor,后面有代码--> <bean id="factoryPostProcessor" class="com.test.spring.FactoryPostProcessor"/> <!-- 普通bean--> <bean id="beanFactoryPostProcessorTest" class="com.test.spring.BeanFactoryPostProcessorTest"> <property name="name" value="张三"/> <property name="sex" value="男"/> </bean> </beans>
按 Ctrl+C 复制代码


这个可以略过 BeanPostProcessor.java
复制代码
package com.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
 * bean后置处理器
 * @author zss
 *
 */
public class PostProcessor implements BeanPostProcessor{

    @Override
    public Object postProcessBeforeInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器处理bean=【"+beanName+"】开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }
}
复制代码
这个大概看一下即可,主要关注它继承了谁,并且关注他的日志,BeanFactoryPostProcessor.java
按 Ctrl+C 复制代码
package com.test.spring; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class FactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { System.out.println("******调用了BeanFactoryPostProcessor"); String[] beanStr = configurableListableBeanFactory .getBeanDefinitionNames(); for (String beanName : beanStr) { if ("beanFactoryPostProcessorTest".equals(beanName)) { BeanDefinition beanDefinition = configurableListableBeanFactory .getBeanDefinition(beanName); MutablePropertyValues m = beanDefinition.getPropertyValues(); if (m.contains("name")) { m.addPropertyValue("name", "赵四"); System.out.println("》》》修改了name属性初始值了"); } } } } }
按 Ctrl+C 复制代码
这个就是一个普通的测试类,关注一下他的属性。BeanFactoryPostProcessorTest.java 
按 Ctrl+C 复制代码
package com.test.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class BeanFactoryPostProcessorTest implements InitializingBean,DisposableBean,BeanNameAware,BeanFactoryAware { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public void setBeanFactory(BeanFactory paramBeanFactory) throws BeansException { System.out.println("》》》调用了BeanFactoryAware的setBeanFactory方法了"); } @Override public void setBeanName(String paramString) { System.out.println("》》》调用了BeanNameAware的setBeanName方法了"); } @Override public void destroy() throws Exception { System.out.println("》》》调用了DisposableBean的destroy方法了"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("》》》调用了Initailization的afterPropertiesSet方法了"); } @Override public String toString() { return "BeanFactoryPostProcessorTest [name=" + name + ", sex=" + sex + "]"; } }
按 Ctrl+C 复制代码

Test case:

复制代码
package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class T {
    ApplicationContext applicationcontext=null;
    @Before
    public void before() {
        System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
        applicationcontext= new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
        System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
    }
    @Test
    public void  test() {
        //BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class);
        BeanFactoryPostProcessorTest beanFactoryPostProcessorTest=applicationcontext.getBean(BeanFactoryPostProcessorTest.class);
        System.out.println(beanFactoryPostProcessorTest.toString());
    }
}
复制代码

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-20 14:36:10  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17ad352e: startup date [Mon Mar 20 14:36:10 CST 2017]; root of context hierarchy
2017-03-20 14:36:10  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [spring-service.xml]
******调用了BeanFactoryPostProcessor
》》》修改了name属性初始值了
》》》调用了BeanNameAware的setBeanName方法了
》》》调用了BeanFactoryAware的setBeanFactory方法了
后置处理器处理bean=【beanFactoryPostProcessorTest】开始
后置处理器开始调用了
》》》调用了Initailization的afterPropertiesSet方法了
后置处理器处理bean=【beanFactoryPostProcessorTest】完毕!
后置处理器调用结束了
》》》Spring ApplicationContext容器初始化完毕了......
BeanFactoryPostProcessorTest [name=赵四, sex=男]
---------------------------------------------------------------------------------------------------------
从测试结果中可以看到beanFactoryPostProcessorTest定义的name值由" 张三"变为"赵四",同时发现postProcessorBeanFactory方法执行顺序先于BeanPostProcessor接口中方法。
好了例子看完了,再去理解那句话“ Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据)”意思是IOC容器允许实现接口 BeanFactoryPostProcessor的类,在web容器开始初始化后,但还没有实例化容器里面所有bean之前,对所有bean配置的元数据也就是bean参数,进行修改:
<bean id="beanFactoryPostProcessorTest" class="com.test.spring.BeanFactoryPostProcessorTest">
           <property name="name" value="张三"/>//这个是元数据
           <property name="sex" value="男"/>//这个是元数据
     </bean>
好了,上面说完BeanFactoryPostProcessor,下面介绍PropertyPlaceholderConfigurer。
2、介绍PropertyPlaceholderConfigurer

在Spring中内置了一些BeanFactoryPostProcessor实现类:

  • org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
  • org.springframework.beans.factory.config.PropertyOverrideConfigurer
  • org.springframework.beans.factory.config.CustomEditorConfigurer:用来注册自定义的属性编辑器

既然 PropertyPlaceholderConfigurer继承了接口BeanFactoryPostProcessor,那它也具备修改IOC容器中bean元数据的功能。
参考:https://www.cnblogs.com/javahr/p/8376742.html
PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它能够对<bean/>中的属性值即元数据进行外在化管理。开发者可以提供单独的属性文件来管理相关属性,下面举例来理解 PropertyPlaceholderConfigurer

属性文件userinfo.properties:
  db.username=scott
  db.password=tiger
下面内容摘自一个完整的spring配置文件,为了理解 PropertyPlaceholderConfigurer可以只看一部分即可。
<bean id="propertyPlaceholderConfigurer"   
        class="org.springframework.beans.factory.config.  
PropertyPlaceholderConfigurer">  
    <property name="locations">  
        <list>  
            <value>userinfo.properties</value>  
        </list>  
    </property>  
</bean>  
 
<bean name="userInfo" class="test.UserInfo">  
  <property name="username" value="${db.username}"/>  
  <property name="password" value="${db.password}"/>  
</bean>
正常情况下,在userInfo的元数据是 username和password,肯定不会出现${db.username}、${db.password}等类似信息,这里采用PropertyPlaceholderConfigurer管理username和password属性的取值。IOC容器实例化userInfo前,PropertyPlaceholderConfigurer会修改userInfo的元数据信息(<bean/>定义),它会用userinfo.properties中db.username对应的scott值替换${db.username}、db.password对应的tiger值替换${db.password}。最终,IOC容器在实例化userInfo时,UserInfo便会得到新的属性值,而不是${db.username}、${db.password}等类似信息。
PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。
3、标签context:property-placeholder
为了为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素,<context:property-placeholder>标签提供了一种优雅的外在化参数配置的方式,不过该标签在Spring配置文件中只能存在一份!!!也就是标签<context:property-placeholder/>相当于在IOC容器中注册了PropertyPlaceholderConfigurer。Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,即只能存在一个实例!

复制代码
<context:property-placeholder   
        location=""  
        file-encoding=""  
        ignore-resource-not-found=""  
        ignore-unresolvable=""  
        properties-ref=""  
        local-override=""  
        system-properties-mode=""  
        order=""  
/> 
复制代码

  (1)location:表示属性文件位置,多个之间通过如逗号/分号等分隔; 
  (2)file-encoding:文件编码; 
  (3)ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常 
  (4)ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常 
  (5)properties-ref:本地java.util.Properties配置 
  (6)local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性 
  (7)system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE 
  (8)ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer 
  (9)OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment 
  (10)NEVER:只查找properties-ref、location; 
  (11)order:当配置多个<context:property-placeholder/>时的查找顺序

使用注意: 

 1、location中的加载文件的顺序

  如果location中有多个文件:

classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties

  将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值 

  举例来说: 
  default.properties文件中有个属性名userId,其对应的值为-1 
  default2.properties文件中也有一个属性名userId,其对应的值为-2 
  default3.properties文件中特有一个属性名userId,其对于那个的值为-3

  default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2 

  所以需要避免不同属性文件中的属性名称重名

2.ignore-resource-not-found和ignore-unresolvable两个属性是类似的作用,推荐配对使用 

  如果location中的文件指向了一个不存在的文件,那么也极有可能意味着有属性无法解析(虽然存在其他属性文件中存在重名,但是这个是应该避免的) 
  所以当ignore-resource-not-found设为true时,ignore-unresolvable也必须设为true,其实当ignore-unresolvable设为true时,ignore-resource-not-found的值true或false,并不影响异常的抛出 
  如果设置为ture,后属性值无法解析成功,将赋值为${属性名} 
  不推荐将ignore-resource-not-found和ignore-unresolvable的值设置为ture,默认为false,可以有效避免程序运行异常

3.properties-ref属性 

  引入其他方式引入的属性文件

复制代码
<bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:default2.properties</value>
        </list>
    </property>
</bean>
复制代码

  该属性需要local-override配合使用,只有当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)

4.local-override属性 

  当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)

5.sytem-properties-mode属性 

  不同的sytem-properties-mode属性定义了不同的查找顺序 
  Environment环境:包括JDK环境,系统环境变量,Sevlet环境,Spring环境等,是Spring在3.1之后抽象的一个表示环境配置

  在local-override属性值为false,sytem-properties-mode属性值为ENVIRONMENT或OVRRIDE时,查找顺序是location,然后是environment或者System.getProperty(),System.getenv()(Spring 3.1 之前) 
  即现加载location指向的属性文件,再加之environment指向的环境,当environment环境中存在和location指向的属性文件中同名的属性,则该属性的值将被修改,取决于environment环境中的值 
  如果sytem-properties-mode属性值为NEVER,则只查询location指向的属性文件

  当local-override属性值为true时,最后将加载properties-ref指向的文件,如遇到同名的,该同名属性值将取决于properties-ref指向的文件中的值

  所以,最终程序中获取的值将是一个综合作用后的值,一般情况下建议sytem-properties-mode属性值为NEVER避免ENVIRONMENT环境中的不可控


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值