您的位置:首页 > 编程语言 > Java开发

spring-PropertyPlaceholderConfigurer和PropertyOverrideConfigurer

2015-08-29 10:30 561 查看
spring的PropertyPlaceholderConfigurer和PropertyOverrideConfigurer,类图如下:



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

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

1)java bean:

[java] view
plaincopy





public class Student {

private String classNo;

private String name;

private int age;

public String getClassNo() {

return classNo;

}

public void setClassNo(String classNo) {

this.classNo = classNo;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

2)属性配置文件config.properties

[html] view
plaincopy





classNo=class1

name=zhangsan

age=15

3)spring的配置

[html] view
plaincopy





<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"

default-autowire="byName">

<bean id="student" class="com.caihj.placeholder.Student">

<property name="classNo" value="${classNo}" />

<property name="name" value="${name}"/>

<property name="age" value="${age}" />

</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>config/config.properties</value>

</list>

</property>

</bean>

</beans>

4)测试类

[java] view
plaincopy





public class Main {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("config/placeholder.xml");

Student bean = (Student) context.getBean("student");

System.out.println(bean.getClassNo());

System.out.println(bean.getName());

System.out.println(bean.getAge());

}

}

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
plaincopy





student.classNo=class1

student.name=zhangsan

3)spring的配置:

[html] view
plaincopy





<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"

default-autowire="byName">

<bean id="student" class="com.ali.caihj.placeholder.Student">

<property name="classNo" value="1班" />

<property name="name" value="张三"/>

<property name="age" value="100" />

</bean>

<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">

<property name="locations">

<list>

<value>config/config.properties</value>

</list>

</property>

</bean>

</beans>

4)测试类,同上面的例子。

运行结果:

[html] view
plaincopy





class1

zhangsan

100

从结果可以看出,student的classNo和name的值被配置文件的值替换了。

注意:使用PropertyOverrideConfigurer类时,properties配置文件的写法和使用PropertyPlaceholderConfigurer类时配置文件的写法不大一样。使用PropertyOverrideConfigurer类时,properties配置文件,key的格式必须为“beanName.propertyName”,如果不是这种格式,则spring容器启动时会报错。而且要确保beanName和propertyName都是存在有效的,否则spring容器启动时也会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: