`

Spring的PropertyPlaceholderConfigurer应用

阅读更多
摘自:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   <property name="location">

     <value>conf/sqlmap/jdbc.properties</value>

   </property>

    <property name="fileEncoding">

      <value>UTF-8</value>

    </property>

</bean>

当然也可以引入多个属性文件,如:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="locations">

   <list>

    <value>/WEB-INF/mail.properties</value> 

    <value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法

   </list>

  </property>

</bean>

3.譬如,jdbc.properties的内容为:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;

jdbc.username=root

jdbc.password=123456

4.那么在spring配置文件中,我们就可以这样写:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="locations">

   <list>

    <value>classpath: conf/sqlmap/jdbc.properties </value>

   </list>

  </property>

</bean>

<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  <property name="driverClassName"value="${jdbc.driverClassName}" />

  <property name="url" value="${jdbc.url}" />

  <property name="username" value="${jdbc.username}"/>

  <property name="password"value="${jdbc.password}" />

</bean>

5.这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。

6.查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。

PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。

我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。
分享到:
评论

相关推荐

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

    在IT行业中,Spring框架是Java应用开发的核心组件,它提供了丰富的功能来简化应用程序的构建。在Spring中,`PropertyPlaceholderConfigurer`是一个非常重要的类,它用于处理属性文件中的占位符,将它们替换为实际的...

    Spring中PropertyPlaceholderConfigurer的使用

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

    Spring属性占位符PropertyPlaceholderConfigurer的使用

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

    4.Spring应用扩展.pptx

    例如,可以将数据库连接信息写入属性文件,如`database.properties`,然后在Spring配置文件中使用`PropertyPlaceholderConfigurer`引入属性文件。这样,我们就可以在XML配置中使用`${...}`的方式来引用属性值,如`${...

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    总之,`org.springframework.beans.factory.config.PropertyPlaceholderConfigurer`是Spring框架中一个核心的配置组件,它简化了应用程序中对环境变量和系统属性的引用,提高了代码的可维护性和安全性。理解并熟练...

    Spring 2企业应用开发[配套源代码]

    Spring框架允许使用`PropertyPlaceholderConfigurer`来替换配置文件中的占位符,例如`${database.url}`,这样可以实现环境间的配置隔离,方便在开发、测试和生产环境之间切换。 3. `applicationContext.xml`:这是...

    SpringFramework应用接入Apollo配置中心过程解析

    SpringFramework应用接入Apollo配置中心过程解析 概述 本文详细介绍了SpringFramework应用接入Apollo配置中心的过程解析,通过示例代码,对大家的学习或者工作具有一定的参考学习价值。本文将从环境配置、...

    spring

    Spring是一个开源的轻量级Java应用开发框架,旨在简化企业级应用程序的开发过程,其核心是控制反转(Inversion of Control, IoC)和面向切面编程(Aspect-Oriented Programming, AOP)。Spring提供了全面的框架支持...

    spring2.5 配置VM

    在Spring框架中,VM选项通常指的是在应用启动时传递给Java虚拟机的一系列系统属性,它们可以影响Spring应用的行为或配置。这些选项可能包括日志级别、类路径设置、服务器端口等。 首先,我们需要理解Spring的配置...

    SPRING:bean配置properties

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

    Spring 容器后处理器

    #### 一、Spring 容器后处理器概念及应用场景 在Spring框架中,容器后处理器(BeanFactoryPostProcessor)是一种非常实用的设计模式,主要用于在容器初始化完成后对容器进行额外的定制操作。这种设计模式不仅提高了...

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

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    spring4.0引用properties

    `PropertyPlaceholderConfigurer`是Spring早期版本中用于注入properties文件中值的bean,而`@ConfigurationProperties`是Spring Boot引入的,更适合现代Spring应用。 使用`PropertyPlaceholderConfigurer`的例子...

    Spring动态加载配置文件

    在Spring框架中,动态加载配置文件是一项重要的功能,它允许我们在程序运行时改变或更新配置,而无需重启应用。这在开发和生产环境中都具有很高的实用价值,尤其是在配置需要频繁调整或者希望实现热更新的场景下。...

    开源框架 Spring Gossip

    管理 Bean 从第一个 Spring 应用程式开始,逐步了解何谓依赖注入,以及如何使用 Spring 的容器功能来管理 Bean,了解 Bean 在 Spring 容器中的生命周期。 第一个 Spring 程式 BeanFactory、 ...

    spring读取properties

    在Spring框架中,读取和使用...在Spring的配置文件中,首先需要定义一个`PropertyPlaceholderConfigurer` bean,这是Spring用来解析Properties文件并将其值注入到其他bean中的关键组件。如示例所示: ```xml ...

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

    18 Spring IoC容器如何读取应用外部的xml,txt,图形或者属性文件?慕课专栏(1)1

    总结起来,Spring提供了多种方式来读取应用外部的配置文件,如`FileSystemXmlApplicationContext`用于读取XML配置,`PropertyPlaceholderConfigurer`用于处理属性文件。理解这一机制有助于我们更灵活地管理应用的...

Global site tag (gtag.js) - Google Analytics