`

Spring框架下PropertyPlaceholderConfigurer类

阅读更多

要了解这个类首先要弄清楚一个概念: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的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。藉由这个类别,您可以将一些组态设定,移出至.properties档案中,如此的安排可以让XML定义档负责系统相关设定,而.properties档可以作为客户根据需求,自定义一些相关的参数。
来看一个Bean定义档的实际例子:

•beans-config.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="configBean"
 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>hello.properties</value>
            </property>
        </bean>

        <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
            <property name="helloWord">
                <value>${onlyfun.caterpillar.helloWord}</value>
            </property>
        </bean>
</beans>
假设在helloBean中有许多依赖注入的属性,这些都是比较不常变动的属性,而其中helloWord会经常变动,可以透过hello.properties来简单的设定,而这个资讯已设定在configBean的location属性中:

•hello.properties
onlyfun.caterpillar.helloWord=Welcome!
在helloBean的helloWord属性中,${onlyfun.caterpillar.helloWord}将会被hello.properties的helloWord所取代。

如果有多个.properties档案,则可以透过locations属性来设定,例如:

•beans-config.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="configBean"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>hello.properties</value>
                    <value>welcome.properties</value>
                    <value>other.properties</value>
   
                </list>
            </property>    
        </bean>

        <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
            <property name="helloWord">
                <value>${onlyfun.caterpillar.helloWord}</value>
            </property>
            ...
        </bean>
</beans> ======================================PropertyPlaceholderConfigurer类就是bean factory post-processor的一种,它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。
例如---spring-context.xml----
 <bean id="propertyConfigurer"     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
           <property name="locations">
               <list>
                   <value>/WEB-INF/jdbc.properties</value>
               </list>
           </property>
 </bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName"><value>${driver}</value></property>
      <property name="url"><value>jdbc:${dbname}</value></property>
</bean>而实际的jdbc.propertis文件是
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
而jdbc.propertis是怎样引用的呢: 将上边一段配置注册在web.xml中就可以了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
当然,不要忘了spring的监听器注册
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这样,一个简单的数据源就设置完毕了。
实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义
的工具。

分享到:
评论

相关推荐

    Spring中PropertyPlaceholderConfigurer的使用

    PropertyPlaceholderConfigurer 是 Spring 框架中的一个重要组件,用于加载和管理 Properties 文件。它能够将 Properties 文件中的键值对注入到 Spring 的 bean 中,从而实现了配置的外部化和动态化。 基本使用方法...

    Spring属性占位符PropertyPlaceholderConfigurer的使用

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

    Spring如何使用PropertyPlaceholderConfigurer读取文件

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

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

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

    最新spring框架学习笔记(全)资料.pdf

    Spring框架的核心是应用上下文(ApplicationContext),它作为一个容器,负责管理和装配应用程序中的各种组件,如bean。 Spring框架并不专注于提供特定功能,如Hibernate用于数据库操作,Struts用于HTTP请求处理。...

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

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

    开源框架 Spring Gossip

    以 Tiles 为例 自订 View Class 与其它 Web 框架的整合 您可以将 Spring 与现在的一些 Web 框架结合在一起,重点都在于如何让 Web 框架意识到 Spring 的存在。 第一个 Struts 程式 在 Struts...

    最新spring框架学习笔记(全)资料 (2).pdf

    Spring框架的核心是应用上下文(ApplicationContext),它扮演着组件管理和配置的角色。在Spring中,应用上下文负责加载XML文件或注解中的配置信息,管理各种Bean,即Java对象,使得开发者可以通过声明式的方式来...

    spring-reference

    Spring框架允许开发者自定义Bean类,并通过BeanDefinition将其配置到容器中。 ##### 3.2.4 Bean 标识符 (id 和 name) Bean的标识符用于唯一标识一个Bean。Spring框架允许使用id或name属性来为Bean指定标识符。 ###...

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

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

    spring整合三大框架笔记

    在现代Java Web开发中,Spring框架作为核心框架,常与其他多个框架进行整合,以实现更强大的功能和服务。本节将详细介绍如何将Spring与Struts2、Hibernate以及相关的连接池等组件进行整合。 #### 二、整合Struts2 ...

    Struts+Spring+Ibatis整合框架搭建配置文档

    添加Spring框架** 为了使Spring能够在Web应用中生效,首先需要在`web.xml`中进行相应的配置。具体步骤包括: - **定义Spring的配置参数**:通过`&lt;context-param&gt;`元素来指定Spring的配置文件路径。在本例中,配置...

    Spring MVC 基本框架

    这些属性可以被 Spring 的 `PropertyPlaceholderConfigurer` 或 `Environment` 类读取,然后在 Spring 配置文件中引用,例如 `applicationContext.xml`: ```xml &lt;bean id="propertyConfigurer" class="org.spring...

    spring

    标题:Spring框架 描述:“just a try!”表明这是一次尝试性的学习或探索Spring框架的过程。 **Spring框架关键知识点解析** Spring是一个开源的轻量级Java应用开发框架,旨在简化企业级应用程序的开发过程,其...

    spring源码经典实例

    jpetstore是Spring框架的经典示例项目,它是一个基于MVC架构的宠物商店应用程序。这个项目由Spring创始人Rod Johnson创建,用于展示Spring框架的核心功能,如依赖注入、AOP(面向切面编程)、数据访问以及Web MVC等...

    spring2.5 配置VM

    标题“spring2.5配置VM”指的是在Spring框架2.5版本中,如何配置虚拟机参数(Virtual Machine,VM)或者Spring的VM选项。在Spring框架中,VM选项通常指的是在应用启动时传递给Java虚拟机的一系列系统属性,它们可以...

    Spring 容器后处理器

    在Spring框架中,容器后处理器(BeanFactoryPostProcessor)是一种非常实用的设计模式,主要用于在容器初始化完成后对容器进行额外的定制操作。这种设计模式不仅提高了系统的灵活性,还增强了系统的可配置性。 ####...

Global site tag (gtag.js) - Google Analytics