spring-PropertyPlaceholderConfigurer和PropertyOverrideConfigurer

spring的PropertyPlaceholderConfigurer和PropertyOverrideConfigurer,类图如下:

PropertyPlaceholderConfigurer和PropertyOverrideConfigurer都是spring提供的用来设置bean属性的类,从类图可以看出,这两个类都实现了BeanFactoryPostProcessor接口。BeanFactoryPostProcessor能够修改bean配置文件中的bean的定义,实现该接口,使得我们能够进行一些额外的处理。

1、使用PropertyPlaceholderConfigurer类,可以利用属性文件为bean设置属性值。例子如下:

1)java bean:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Student {  
  2.     private String classNo;  
  3.     private String name;  
  4.     private int    age;  
  5.   
  6.     public String getClassNo() {  
  7.         return classNo;  
  8.     }  
  9.     public void setClassNo(String classNo) {  
  10.         this.classNo = classNo;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public int getAge() {  
  19.         return age;  
  20.     }  
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24. }  
2)属性配置文件config.properties

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. classNo=class1  
  2. name=zhangsan  
  3. age=15  
3)spring的配置

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
  9.     default-autowire="byName">  
  10.     <bean id="student" class="com.caihj.placeholder.Student">  
  11.         <property name="classNo" value="${classNo}" />  
  12.         <property name="name" value="${name}"/>  
  13.         <property name="age" value="${age}" />  
  14.     </bean>  
  15.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  16.         <property name="locations">  
  17.             <list>  
  18.                 <value>config/config.properties</value>  
  19.             </list>  
  20.         </property>  
  21.      </bean>    
  22. </beans>  
4)测试类

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Main {  
  2.   
  3.     public static void main(String[] args) {  
  4.         ApplicationContext context = new ClassPathXmlApplicationContext("config/placeholder.xml");  
  5.         Student bean = (Student) context.getBean("student");  
  6.         System.out.println(bean.getClassNo());  
  7.         System.out.println(bean.getName());  
  8.         System.out.println(bean.getAge());  
  9.   
  10.     }  
  11. }  
spring容器在启动的时候,AbstractApplicationContext类,通过beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false),自动获取spring配置文件中所有实现BeanFactoryPostProcessor接口的bean,并且对于每个bean,调用postProcessBeanFactory方法,修改相关bean的属性值。PropertyPlaceholderConfigurer类,将以“${”开头,以"}"结束的属性值进行处理,并将properties配置文件中相同名字的属性对应的值,取代占位符信息。

2、使用PropertyOverrideConfigurer类,可以利用属性文件设置的属性值,覆盖spring配置文件定义的bean的属性值。例子如下:

1)java bean,同上面的Student类。

2)属性配置文件config.properties

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. student.classNo=class1  
  2. student.name=zhangsan  
3)spring的配置:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
  9.     default-autowire="byName">  
  10.     <bean id="student" class="com.ali.caihj.placeholder.Student">  
  11.         <property name="classNo" value="1班" />  
  12.         <property name="name" value="张三"/>  
  13.         <property name="age" value="100" />  
  14.     </bean>     
  15.     <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">  
  16.         <property name="locations">  
  17.             <list>  
  18.                 <value>config/config.properties</value>  
  19.             </list>  
  20.         </property>  
  21.      </bean>  
  22. </beans>  
4)测试类,同上面的例子。
运行结果:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class1  
  2. zhangsan  
  3. 100  
从结果可以看出,student的classNo和name的值被配置文件的值替换了。

注意:使用PropertyOverrideConfigurer类时,properties配置文件的写法和使用PropertyPlaceholderConfigurer类时配置文件的写法不大一样。使用PropertyOverrideConfigurer类时,properties配置文件,key的格式必须为“beanName.propertyName”,如果不是这种格式,则spring容器启动时会报错。而且要确保beanName和propertyName都是存在有效的,否则spring容器启动时也会报错。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值