spring的PropertyPlaceholderConfigurer和PropertyOverrideConfigurer

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;
    }
}
2)属性配置文件config.properties

classNo=class1
name=zhangsan
age=15
3)spring的配置

<?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)测试类

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

student.classNo=class1
student.name=zhangsan
3)spring的配置:

<?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)测试类,同上面的例子。
运行结果:

class1
zhangsan
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、付费专栏及课程。

余额充值