PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现。关于BeanFactoryPostProcessor和BeanPostProcessor类似。我会在其他地方介绍。
PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。这样的话,我只需要对properties文件进行修改,而不用对xml配置文件进行修改。
作用是什么呢?
有一些属性值不需要经常变更,但是有一些属性值可能随时改变,把经常会改动的属性值放在另一个文件中的话,程序使用起来也更方便。
下面将通过一个例子来理解PropertyPlaceholderConfigurer。
首先是配置文件,代码如下:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="propertyPlaceHolder.properties"/>
</bean>
<bean id ="student" class="co.jp.beanFactoryPost.Student">
<property name="name">
<value>${name}</value>
</property>
<property name="age">
<value>${age}</value>
</property>
<property name="birth">
<value>${birth}</value>
</property>
</bean>
</beans>
在这个配置文件中最重要的就是下面这段代码,
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="propertyPlaceHolder.properties"/>
</bean>
PropertyPlaceholderConfigurer会读取location所设定的属性文件,并将properties文件中的值,设定给${name},${age},${birth}从而完成依赖注入。
相应的properties文件的定义如下:
name=xiaohailin
age=27
birth=19820123
这里用到的student bean,的class的代码如下:
比较简单,主要是一个get,set方法
package co.jp.beanFactoryPost;
public class Student {
private String name;
private String age;
private String birth;
public void setName(String name)
{
this.name = name;
}
public void setAge(String age)
{
this.age = age;
}
public void setBirth(String birth)
{
this.birth = birth;
}
public String getName()
{
return this.name;
}
public String getAge()
{
return this.age;
}
public String getBirth()
{
return this.birth;
}
}
接着,写一个测试的主类,代码如下:
public class PropertyHolderPlaceDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("propertyPlaceHolder.xml");
Student student = (Student) ctx.getBean("student");
System.out.println(student.getAge());
System.out.println(student.getBirth());
System.out.println(student.getName());
}
}
更多详细信息请查看 Spring 教程 http://www.itchm.com/forum-59-1.html
分享到:
相关推荐
Spring中PropertyPlaceholderConfigurer的使用
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.1.1. 使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7....
13 Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x ..................................................
4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:BeanPostProcessor 风格的Hello World.. 79 4.8.1.2 示例:RequiredAnnotationBeanPostProcessor 81 4.8.2 ...
6 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 7 8 9 <value>/WEB-INF/jdbc.properties 10 11 12 13 14 它配置了以下功能: 读取...
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
13 Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x ..................................................
简介 Spring Inversion of Control Dependency Injection <br> 核心容器 Spring 核心容器实作了 IoC,BeanFactory 与 ApplicationContext 的运用是了解 Spring 的重点所在。 管理 Bean...
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath*:jdbc.properties <!-- 数据源配置,主要用于开发测试...
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面...<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:proxool.properties ...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>WEB-INF/jdbc.properties class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
ControlFlowFactory.Jdk13ControlFlow ControlFlowFactory.Jdk14ControlFlow ControlFlowPointcut Controller Controller ControllerClassNameHandlerMapping Conventions CookieGenerator ...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/deploy.properties <!-- 配置sessionFactory ...