PropertyPlaceholderConfigurer

转:

http://blog.csdn.net/sz_bdqn/article/details/6666262

要了解这个类首先要弄清楚一个概念:bean factory post-processor
官方解释是这样的:
A bean factory post-processor is a java class which implements the
org.springframework.beans.factory.config.BeanFactoryPostProcessor interface. It is executed manually (in the case of the BeanFactory) or automatically (in the case of the ApplicationContext) to apply changes of some sort to an entire BeanFactory, after it has been constructed.
我理解的意思是这样的:
1.首先bean factory post-processor实现了org.springframework.beans.factory.config.BeanFactoryPostProcessor接口。
2.在BeanFactory的情况下它被手动的执行。
3.在ApplicationContext的条件下它会自动的执行。
4.最关键的一点是,是在一个类的实例被构造出来之后,对整个BeanFactory进行修改。

spring属性占位符PropertyPlaceholderConfigurer的使用

1、一个简单的Demo

1.1、创建conf.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 
        使用location属性定义单个配置文件
        <property name="location">
            <value>classpath:/com/zsw/config/jdbc.properties</value>
        </property>
         -->
  
  <!-- 使用locations属性定义多个配置文件 -->
  <property name="locations">
     <list>
        <value>classpath:/com/zsw/config/jdbc.properties</value>
     </list>
  </property> 
    </bean>
    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url">
            <value>${database.url}</value>
        </property>
        <property name="driverClassName">
            <value>${database.driver}</value>
        </property>
        <property name="username">
            <value>${database.user}</value>
        </property>
        <property name="password">
            <value>${database.password}</value>
        </property>
    </bean>
</beans>

1.2.创建jdbc.properties文件

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/right?useUnicode=true&autoReconnect=true&characterEncoding=UTF-8
database.user=root
database.password=root
jdbc.pool.c3p0.acquire_increment=2
jdbc.pool.c3p0.max_size=20
jdbc.pool.c3p0.min_size=2
jdbc.pool.c3p0.preferred_test_query=‘SELECT 1‘
jdbc.pool.c3p0.idle_connection_test_period=18000
jdbc.pool.c3p0.max_idle_time=25000

1.3.创建Config.Java

package com.zsw.config;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class Config {
     public static void main(String[] args) {
         XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/com/zsw/config/conf.xml"));
         // 如果要在BeanFactory中使用,bean factory post-processor必须手动运行:
         PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
         cfg.setLocation(new FileSystemResource("src/com/zsw/config/jdbc.properties"));
         cfg.postProcessBeanFactory(factory);
         DriverManagerDataSource dataSource = (DriverManagerDataSource) factory.getBean("dataSource");
//         System.out.println(dataSource.getDriverClassName());
         
         
         System.out.println(dataSource.getUsername());
         // 注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean
         // factory postprocessor的用户使用ApplicationContext代替BeanFactroy。
         ApplicationContext context = new ClassPathXmlApplicationContext("com/zsw/config/conf.xml");
         DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context.getBean("dataSource");
         System.out.println(dataSource2.getUsername());
     }
 
}

2.Spring中PropertyPlaceholderConfigurer多种配置方式

2.1配置单个Properties文件

<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:/spring/include/dbQuery.properties</value>
    </property>
</bean>

其中classpath是引用src目录下的文件写法。

2.2 当存在多个Properties文件时,配置就需使用locations了:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
       <list>
          <value>classpath:/spring/include/jdbc-parms.properties</value>
          <value>classpath:/spring/include/base-config.properties</value>
        </list>
    </property>
</bean>

2.3、接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties 文件,其配置如下:

<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="location">
       <value>classpath:/spring/include/dbQuery.properties</value>
    </property>
</bean>

<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="2" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="locations">
      <list>
        <value>classpath:/spring/include/jdbc-parms.properties</value>
        <value>classpath:/spring/include/base-config.properties</value>
      </list>
    </property>
</bean>

其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true

时间: 2024-08-10 00:07:13

PropertyPlaceholderConfigurer的相关文章

spring PropertyPlaceholderConfigurer 找不到配置文件原因

1:  spring 版本问题 参见: http://www.cnblogs.com/alex-blog/archive/2012/12/25/2832357.html 2: bean id 同名  看看工程中引入的其他配置文件是否也是configBean 建议不同工程 不同id 名 <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfi

Spring的PropertyPlaceholderConfigurer应用

转载地址:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html Spring 利用PropertyPlaceholderConfigurer占位符 1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标

Spring PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer是BeanFactoryPostProcessor的子类. 通常情况下,我们不想将类似于系统管理相关的信息同业务对象相关的配置信息混杂到XML配置文件中,以免部署或维护期间因为改动复杂的XML配置文件而出现问题.我们会将一些数据库连接信息.邮件服务器等相关信息单独配置到一个properties文件中,这样,如果因系统资源变动的话,只需要关注这些简单的properties配置文件即可. PropertyPlaceholderConfigur

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去.在XML文件中用${key}替换指定的properties文件中的值.这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改. 从上图中,我们看到PropertyPlaceholderConfigurer实现了三个bean生命周期的接口:BeanFactoryAware & BeanNameAware & BeanFactoryPostProcessor.关于sprin

使用 Spring Boot 快速构建 Spring 框架应用,PropertyPlaceholderConfigurer

Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.Spring 框架包含几十个不同的子项目,涵盖应用开发的不同方面.如此多的子项目和组件,一方面方便了开发人员的使用,另外一个方面也带来了使用方面的问题.每个子项目都有一定的学习曲线.开发人员需要了解这些子项目和组件的具体细节,才能知道如何把这些子项目整合起来形成一个完整的解决方案.在如何使用这些组件上

Spring属性占位符PropertyPlaceholderConfigurer的使用

1.一个简单的Demo 1.1.创建conf.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <bean id="

二 IOC之PropertyPlaceholderConfigurer

老长一段时间没有看文档了,今天看到这个PropertyPlaceholderConfigurer有点意思,我于百忙之中抽出点时间,将这个点记录在这里,方便日后慢慢完善,慢慢深入. 因为我目前看的都是based-XML的方式,所以先看下配置是如何的吧! <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="pro

PropertyPlaceholderConfigurer读取配置文件

1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去.在XML文件中用${key}替换指定的properties文件中的值.这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改. 2.在Spring

spring的多个PropertyPlaceholderConfigurer实例装配的问题

1. 默认情况下,使用PropertyPlaceholderConfigurer多实例装配出现异常 在项目中尝试 在不同的spring的配置文件中分别引入相应的properties文件,这样会在spring配置文件中配置多个PropertyPlaceholderConfigurer实例,但是这样使用的话就会出现key找不到的问题,异常信息如下: " Could not resolve placeholder 'key2" 信息: Destroying singletons in org