spring的PropertyPlaceholderConfigurer和PropertyOverrideConfigurer,类图如下:
PropertyPlaceholderConfigurer和PropertyOverrideConfigurer都是spring提供的用来设置bean属性的类,从类图可以看出,这两个类都实现了BeanFactoryPostProcessor接口。BeanFactoryPostProcessor能够修改bean配置文件中的bean的定义,实现该接口,使得我们能够进行一些额外的处理。
1、使用PropertyPlaceholderConfigurer类,可以利用属性文件为bean设置属性值。例子如下:
1)java bean:
- 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;
- }
- }
- classNo=class1
- name=zhangsan
- age=15
- <?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>
- 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());
- }
- }
2、使用PropertyOverrideConfigurer类,可以利用属性文件设置的属性值,覆盖spring配置文件定义的bean的属性值。例子如下:
1)java bean,同上面的Student类。
2)属性配置文件config.properties
- student.classNo=class1
- student.name=zhangsan
- <?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>
运行结果:
- class1
- zhangsan
- 100
注意:使用PropertyOverrideConfigurer类时,properties配置文件的写法和使用PropertyPlaceholderConfigurer类时配置文件的写法不大一样。使用PropertyOverrideConfigurer类时,properties配置文件,key的格式必须为“beanName.propertyName”,如果不是这种格式,则spring容器启动时会报错。而且要确保beanName和propertyName都是存在有效的,否则spring容器启动时也会报错。