`

PropertyPlaceholderConfigurer Usage

阅读更多

 

The PropertyPlaceholderConfigurer is used to externalize property values from a BeanFactory definition, into another separate file in the standard Java Properties format.

 

========Spring Config snippet==================

 

 

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

    <property name="locations">

        <value>classpath:com/foo/jdbc.properties</value>

    </property>

</bean>

 

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

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

 

 

 

 

 =========================================

 

Alternatively, you can also config the "placeholderconfigurer" like this. Pay attention: the bean acutally is never referenced by somebody, it's there, and it works.

 

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

<property name="searchSystemEnvironment" value="true"/> 

<property name="location" value="classpath:application.properties"/>

</bean>

 

 =========================================

 

 

The actual values come from another file in the standard Java Properties format:

 

 

jdbc.driverClassName=org.hsqldb.jdbcDriver

jdbc.url=jdbc:hsqldb:hsql://production:9002

jdbc.username=sa

jdbc.password=root

 

 

 =========================================

 

As ablove, as long as you've configged the PropertyPlaceholderconfigurer bean, you can just use ${**} variables, configed in the jdbc.properties.

 

 The PropertyPlaceholderConfigurer doesn't only look for properties in the Properties file you specify, but also checks against the Java System properties if it cannot find a property you are trying to use. This behavior can be customized by setting the systemPropertiesMode property of the configurer.

 

=======================================

Another thing to elaborate:

1) ApplicationContext -- no need for manual call.

2) BeanFactory -- you need to call propertyPlaceholderConfigurer.postProcessBeanFactory(factory); to make it work. Please refer to: http://www.cublog.cn/u/9295/showart_261437.html

分享到:
评论

相关推荐

    Spring中PropertyPlaceholderConfigurer的使用

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

    Spring属性占位符PropertyPlaceholderConfigurer的使用

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

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

    在Spring中,`PropertyPlaceholderConfigurer`是一个非常重要的类,它用于处理属性文件中的占位符,将它们替换为实际的值。这在配置管理中起到了关键作用,特别是在大型分布式系统中,动态配置管理变得尤为重要。...

    Spring如何使用PropertyPlaceholderConfigurer读取文件

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

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    `org.springframework.beans.factory.config.PropertyPlaceholderConfigurer` 是Spring框架中的一个重要组件,主要负责处理配置文件中的占位符替换。这个类是Spring在初始化bean时用来解析和注入环境变量或系统属性...

    SPRING:bean配置properties

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

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

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

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

    Spring 2.0引入了`PropertyPlaceholderConfigurer`类,它是一个Bean工厂后处理器,负责在Bean定义中替换以`${...}`形式的占位符为实际的属性值。这些属性通常来自一个或多个`.properties`文件,可以是classpath下的...

    SSM 读取properties文件

    在Spring框架中,读取properties文件主要通过两种方式:`PropertyPlaceholderConfigurer` 和 `@Value` 注解。 1. **PropertyPlaceholderConfigurer**: 这是一个Spring的bean定义类,它允许我们从properties文件中...

    java 获取properties的几种方式(csdn)————程序.pdf

    Spring提供了`PropertyPlaceholderConfigurer`类,它可以将Properties文件中的值注入到Bean的属性中。在XML配置文件中,我们创建一个`PropertyPlaceholderConfigurer`的Bean,并指定`location`属性为Properties文件...

    Spring 容器后处理器

    `PropertyPlaceholderConfigurer`是一个常用的容器后处理器,它的主要作用是从外部的属性文件中读取配置信息,并将这些配置信息插入到BeanFactory的定义中。这使得可以在不修改主配置文件的情况下更改某些配置值,如...

    说说在Spring中如何引用外部属性文件的方法

    除了使用 PropertyPlaceholderConfigurer 之外,我们还可以使用 context 命名空间来定义属性文件,相对于 PropertyPlaceholderConfigurer 的配置方式,这种方式更优雅。 ``` file-encoding="utf-8"/&gt; ``` 在 ...

    JDBC Template源码.7z

    本文将深入探讨JDBC Template的源码,结合MySQL数据库,展示其在实际应用中的使用,并提及Spring框架中的`PropertyPlaceholderConfigurer`和`BeanPropertyRowMapper`组件。 首先,JDBC Template通过预编译SQL语句、...

    spring配置文件

    `PropertyPlaceholderConfigurer`是Spring提供的一个实用工具类,它允许我们在配置文件中使用占位符 `${...}` 来引用外部属性文件中的值。在示例中,`location`属性指定了属性文件的位置,如`classpath:/spring/...

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

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

    spring4.0引用properties

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

    SpringValue注解

    在上面的配置中,我们定义了一个名为 appProperty 的 Bean,该 Bean 使用 PropertyPlaceholderConfigurer 类来加载名为 config.properties 的 properties 文件。 使用 @Value 注解 在 Bean 中,可以使用 @Value ...

    属性占位符配置器

    2. **配置PropertyPlaceholderConfigurer**:在Spring配置文件中,通过`&lt;bean&gt;`元素定义一个`PropertyPlaceholderConfigurer`实例,并设置其`location`属性指向外部属性文件的位置。 3. **使用占位符**:在其他Bean...

    spring 启动时加载不同的文件

    - **定义**: `PropertyPlaceholderConfigurer`是Spring框架提供的用于解析`properties`文件中定义的占位符的工具类。 - **功能**: 它能够将`properties`文件中的键值对映射到Spring容器中,供其他Bean使用。 **2. ...

    spring读取properties

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

Global site tag (gtag.js) - Google Analytics