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容器启动时也会报错。
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容器启动时也会报错。
相关文章推荐
- Java 温故
- 在ubuntu下使用Eclipse搭建Hadoop开发环境《转》
- 从Eclipse到Android Studio——改变了什么
- Java记录 -12- 方法重载与重写
- 如何构建自己的.jar文件
- Leetcode全排列问题Java版
- 汇总常见的JAVA错误
- JAVA生成给定范围内的不重复的随机数的几种方法
- 杭电ACM2051java做法
- java综合(五)spring与hibernate整合-事务
- JavaWeb应用中错误和异常处理方法研究
- 【java】基础问题
- 编译、解释与JIT
- date and time
- 省电管理之电源电量显示分析JAVA层
- Java使用SAX,XmlPull两种方式解析XML
- Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- java多线程 —— 为什么弃用stop和suspend
- [Java Concurrency in Practice]第七章 取消与关闭
- Struts(3)的工作机制及分析