`
xinklabi
  • 浏览: 1596438 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
文章分类
社区版块
存档分类
最新评论

Spring:PropertyPlaceholderConfigurer的使用

阅读更多

PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。这样的话,我只需要对properties文件进行修改,而不用对xml配置文件进行修改。

作用是什么呢?

有一些属性值不需要经常变更,但是有一些属性值可能随时改变,把经常会改动的属性值放在另一个文件中的话,程序使用起来也更方便。

 

PropertyPlaceholderConfigurer可以读取在Properties文件中的配置。

applicationContext.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
  <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
  <list>
   <value>/init.properties</value>
  </list>
 </property>
  </bean>
  <!-- Choose the dialect that matches your "dataSource" definition -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName">
   <value>${datasource.driverClassName}</value>
   <!--com.mysql.jdbc.Driver-->
  </property>
  <property name="url">
   <value>${datasource.url}</value>
   <!--jdbc:mysql://localhost:3306/example-->
  </property>
  <property name="username">
   <value>${datasource.username}</value>
   <!--root-->
  </property>
  <property name="password">
   <value>${datasource.password}</value>
   <!--1234-->
  </property>
  <property name="maxActive">
   <value>${datasource.maxActive}</value>
   <!--10-->
  </property>
  <property name="maxIdle">
   <value>${datasource.maxIdle}</value>
   <!--2-->
  </property>
  <property name="maxWait">
   <value>${datasource.maxWait}</value>
   <!--120000-->
  </property>
  <property name="defaultAutoCommit">
   <value>${datasource.defaultAutoCommit}</value>
   <!--true-->
  </property>
 </bean>
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
      <list>
        <value>com/fitech/example/spring/bean/TestBean.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop><!--org.hibernate.dialect.MySQLDialect-->
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop><!--true-->
        <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop><!--50-->
        <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop><!--25-->
      </props>
    </property>
  </bean>
  <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
  <!-- <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    <property name="sessionFactory">
      <ref local="mySessionFactory"/>
    </property>
  </bean> -->
  <bean id="testBeanDao" class="com.fitech.example.spring.dao.hibernate.TestBeanDao">
    <property name="sessionFactory">
      <ref local="mySessionFactory"/>
    </property>
  </bean>
 
  <bean id="caculateService" class="com.fitech.example.spring.business.impl.CaculateImpl">
    <property name="testBeanDao">
      <ref local="testBeanDao"/>
    </property>
  </bean>
 
  <bean id="listService" class="com.fitech.example.spring.business.impl.ListAllImpl">
    <property name="testBeanDao">
      <ref local="testBeanDao"/>
    </property>
  </bean>
</beans>

 

init.properties

datasource.driverClassName=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/example?useUnicode=true&characterEncoding=utf8
datasource.username=root
datasource.password=1234

#datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
#datasource.url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbscs
#datasource.username=sa
#datasource.password=

datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000

#datasource.defaultAutoCommit=true
datasource.defaultAutoCommit=false

datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false

hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
#hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect

hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

Test.java

Resource res = new ClassPathResource("applicationContext.xml");
  BeanFactory factory = new XmlBeanFactory(res);

  /*不在applicationContext.xml配置PropertyPlaceholderConfigurer 
   * Properties props = new Properties();
   *
   * props.load(new FileInputStream("init.properties"));
   * PropertyPlaceholderConfigurer cfg = new
   * PropertyPlaceholderConfigurer(); cfg.setProperties(props);
   */

  //配置PropertyPlaceholderConfigurer 
  PropertyPlaceholderConfigurer cfg = (PropertyPlaceholderConfigurer) factory
    .getBean("placeholderConfig");
  cfg.postProcessBeanFactory((XmlBeanFactory) factory);

  IfCaculate caculateService = (IfCaculate) factory
    .getBean("caculateService");
  caculateService.doPlus(123, 321);
  caculateService.doMultiply(123, 321);

  IfListAll listAll = (IfListAll) factory.getBean("listService");
  List all = listAll.listAll();
  for (Iterator iter = all.iterator(); iter.hasNext();) {
   TestBean tb = (TestBean) iter.next();
   System.out.println(tb.getId() + "," + tb.getParam1() + ","
     + tb.getParam2() + "," + tb.getValue());
  }

 

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

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

 

 

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

 

<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>

 

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

 

<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

 

至此你已经了解到了如何使用PropertyPlaceholderConfigurer,如何使用多个Properties文件,以及如何配置多个PropertyPlaceholderConfigurer来分解工程中分散的Properties文件。至于 PropertyPlaceholderConfigurer还有更多的扩展应用,如属性文件加密解密等方法

分享到:
评论

相关推荐

    Spring中PropertyPlaceholderConfigurer的使用

    Spring 中 PropertyPlaceholderConfigurer 的使用 PropertyPlaceholderConfigurer 是 Spring 框架中的一个重要组件,用于加载和管理 Properties 文件。它能够将 Properties 文件中的键值对注入到 Spring 的 bean 中...

    Spring属性占位符PropertyPlaceholderConfigurer的使用

    在Spring框架中,属性占位符`PropertyPlaceholderConfigurer`是一个重要的工具,用于处理配置文件中的属性值引用。它使得我们可以在XML配置文件中使用占位符`${...}`来引用外部属性文件中的值,从而使应用配置更加...

    SPRING:bean配置properties

    在Spring框架中,Bean的配置与管理是其核心功能之一,而通过`PropertyPlaceholderConfigurer`进行属性占位符的配置则是实现动态配置的关键技术。本文将深入解析如何利用`PropertyPlaceholderConfigurer`进行bean配置...

    Spring PropertyPlaceholderConfigurer配置文件加载器集成ZooKeeper来实现远程配置读取

    以上就是关于"Spring PropertyPlaceholderConfigurer配置文件加载器集成ZooKeeper来实现远程配置读取"的详细解释,涵盖了Spring的配置处理、ZooKeeper的使用以及两者结合的实现过程。理解并掌握这一技术,有助于提升...

    Spring如何使用PropertyPlaceholderConfigurer读取文件

    Spring如何使用PropertyPlaceholderConfigurer读取文件 Spring框架中,PropertyPlaceholderConfigurer是一个非常重要的组件,它可以帮助我们读取配置文件,实现系统的配置信息统一管理。在大型项目中,我们往往会将...

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    `spring1.docx`和`spring2.docx`可能是两份关于Spring框架的文档,可能包含了更多关于`PropertyPlaceholderConfigurer`的使用示例、最佳实践以及与其他Spring特性(如`@Value`注解、`@ConfigurationProperties`等)...

    关于spring系统中多系统的配置

    在Spring框架中,`PropertyPlaceholderConfigurer`是一种特殊的Bean,它被用来处理Spring配置文件中的占位符(placeholder),并将它们替换为具体的值。这些值通常来自外部的属性文件,如`.properties`或`.xml`等。...

    SPRING中文开发参考手册

    - **通过实例工厂方法创建 Bean**:使用非静态工厂方法来创建 Bean 实例。 - **Bean的标识**:每个 Bean 都有一个唯一的标识符(id 或 name)。 - **Singleton 的使用与否**:默认情况下,Bean 采用 Singleton ...

    开源框架 Spring Gossip

    不使用XML定义档进行 Bean设置 Aware 相关介面 BeanPostProcessor BeanFactoryPostProcessor PropertyPlaceholderConfigurer PropertyOverrideConfigurer CustomEditorConfigurer ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    在Spring中使用加密外部属性文件

    Spring默认的`PropertyPlaceholderConfigurer`不直接支持加密的属性文件,但它提供了扩展点,允许我们自定义实现来处理加密后的属性。以下是一种实现方式: 1. 创建一个自定义的`PropertyPlaceholderConfigurer`...

    spring中 连接池的使用

    在Spring框架中,数据库连接池是管理数据库连接的关键组件,它能有效地提高应用程序的性能和资源利用率。...同时,随着Spring的发展,现在更推荐使用HikariCP作为默认的连接池,因为它在性能和稳定性上表现更优秀。

    掌握Spring设计模式:Java工程师必备指南

    此外,`BeanFactoryPostProcessor`接口允许自定义扩展,如`PropertyPlaceholderConfigurer`用于处理配置文件中的占位符。 2. **工厂方法**:Spring提供的`FactoryBean`接口是工厂方法模式的体现。它使得我们能够...

    spring

    - **依赖检查**:Spring可以检查应用程序中是否存在未使用的Bean引用,帮助开发者发现潜在的问题。 #### 3.4 定制Bean的行为 - **生命周期接口**:Spring定义了一系列的生命周期回调接口,如`InitializingBean`和`...

    spring4.0引用properties

    接下来,为了在Spring 4.0中引用这些属性,我们需要配置一个`PropertyPlaceholderConfigurer`或使用`@ConfigurationProperties`注解。`PropertyPlaceholderConfigurer`是Spring早期版本中用于注入properties文件中值...

    spring使用属性文件

    下面将详细介绍如何在Spring中使用属性文件以及相关知识点。 1. **属性文件格式** 属性文件通常以`.properties`为扩展名,例如`application.properties`或`database.properties`。文件中的键值对以等号`=`分隔,如...

    springmvc+spring+mybatis

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置数据源 --&gt; ${driverClassName}"/&gt; ${url}"/&gt; ${username...

    Spring 容器后处理器

    ### Spring 容器后处理器详解 #### 一、Spring 容器后处理器概念及应用...此外,使用诸如`PropertyPlaceholderConfigurer`这样的后处理器还可以提高配置管理的效率,使得应用程序能够在不同的环境中更加灵活地运行。

    spring2.5 配置VM

    综上所述,Spring 2.5配置VM涉及到的是如何通过Java虚拟机参数来影响Spring应用的运行和配置,以及如何在Spring框架中使用这些参数。同时,`FormBeanUtil.java`文件可能是一个辅助工具,用于处理Spring MVC中的表单...

    基于Spring2.0 Property Placeholder配置的源码例子

    这个例子“基于Spring 2.0 Property Placeholder配置的源码”展示了如何在应用程序中使用Property Placeholder来动态加载和替换配置文件中的属性值,从而实现配置的灵活性和可重用性。下面将详细介绍这个主题。 1. ...

Global site tag (gtag.js) - Google Analytics