`

PropertyPlaceHolderConfigurer usage in my app

阅读更多

Original usage

 

Originally, our application use the org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in this way.

 

In Spring config file:

 

  <bean id="initializationBean" class="com.citigroup.eqtg.exchangesim.InitializationBean">

    <property name="beanFactoryPostProcessor" ref="placeholderConfigurer"/>

  </bean>

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

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

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

  </bean>

 

Something to explain:

 

1)    PlaceHolderConfigurer bean is used to separate env-dependant config/param from actual source code. Specific configuration is used in config file – application.properties.

And in source code, we just use ${database.jdbc.url}, ${database.jbdc.user}, etc.

 

2)    In application.properties

 

application.database.jdbc.url= ...

application.database.user= ...

 

3)    For Beanfactory, PlaceholderConfigurer won’t take effect automatically. You have to mantually coding in this way:

 

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("com/starxing/test/conf.xml"));

PropertyPlaceholderConfigurer cfg = …… // There are 2 ways to instantiate.

cfg.postProcessBeanFactory(factory); 

 

   After this, variables used in spring config has been replaced with real values.

 

4)    For ApplicationContext, there is no need to be this complex. As long as you’ve configged placeholderConfigurer in the spring config file, spring will replace the variables automatically. So, ApplicationContext is strongly preferred.

 

5)    In our application, we configged a initializationBean, the only purpose of this class is to call “cfg.postProcessBeanFactory(factory);”. To do this,

 

a.    It has to get a reference to the Spring BeanFactory, so, it has to implement the BeanFactoryAware interface.

b.    It has to get a reference to the PropertyPlaceholderConfigurer object, so it has to has be property --- beanFactoryPostProcessor.

c.    It has to have a time to call this, so it has to implement the InitializingBean interface.

 

public class InitializationBean implements BeanFactoryAware, InitializingBean {

    private BeanFactory beanFactory;

    private BeanFactoryPostProcessor beanFactoryPostProcessor;

 

    public InitializationBean() {

    }

 

    public void afterPropertiesSet() {

        beanFactoryPostProcessor.postProcessBeanFactory((ConfigurableListableBeanFactory)beanFactory);

    }

 

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

        this.beanFactory = beanFactory;

    }

 

    public void setBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor) {

        this.beanFactoryPostProcessor = beanFactoryPostProcessor;

    }

}

 

So, generally speaking, it’s really very complex the use Beanfactory + PropertyPlaceholderConfigurer.

 

 

Now usage

 

There is no InitializationBean. And we just config the placeholderConfigurer in the spring config file, and it will work.

分享到:
评论

相关推荐

    Spring中PropertyPlaceholderConfigurer的使用

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

    Spring属性占位符PropertyPlaceholderConfigurer的使用

    &lt;prop key="my.default.property"&gt;defaultValue ``` 2. **属性文件的顺序**:如果有多个属性文件,Spring会按照文件位置的顺序依次解析,后一个文件中的同名属性会覆盖前一个文件中的值。 3. **系统属性...

    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 in Action》学习点滴

    ### Spring in Action 学习点滴知识点总结 #### 第二章:装配Bean 1. **`&lt;ref&gt;`标签中的`bean`、`local`、`parent`三个属性的区别** - `bean`属性用于指向一个具体的bean实例。 - `local`属性用于表示本地bean...

    Spring In Action笔记100例

    可以使用`PropertyPlaceholderConfigurer`来载入属性文件,并在其他地方使用`${database.url}`等方式引用属性值。 #### 11. `CustomEditorConfigurer`的使用 通过`CustomEditorConfigurer`可以注册自定义的`...

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

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

    spring boot 注入 property的三种方式(推荐)

    final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setIgnoreResourceNotFound(true); final List&lt;Resource&gt; resourceLst = new ArrayList(); resourceLst.add(new Class...

    redis 安装及使用手册

    2. 引入配置:在 Spring 的 `app-context.xml` 配置文件中,通过 `&lt;import&gt;` 导入 `app-redis.xml`,并使用 `&lt;bean&gt;` 注册 `PropertyPlaceholderConfigurer`,加载 `redis.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的定义中。这使得可以在不修改主配置文件的情况下更改某些配置值,如...

    spring2.5 配置VM

    配置VM选项通常是通过在启动命令行时添加`-D`参数来实现的,例如`java -Dproperty.name=value -jar yourApp.jar`。这里的`property.name`是系统属性的键,`value`是对应的值。 在Spring应用中,这些VM参数可以通过`...

    说说在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`...

Global site tag (gtag.js) - Google Analytics