`
vanhelp.songst
  • 浏览: 66649 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

浅析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进行修改。
     那么PropertyPlaceholderConfigurer类就是bean factory post-processor的一种,它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。
例如
<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是怎样引用的呢:
---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>
将上边一段配置注册在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来实现远程配置读取

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

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

    Spring允许与`xxx.properties`格式的配置文件结合使用,首先在XML中注册配置文件,如`&lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;`,然后在其他bean中引用这些属性值,...

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

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

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

    Spring 框架是Java开发领域中不可或缺的一部分,它的设计理念是为了简化企业级应用的开发,通过提供一个统一的容器来管理对象的生命周期和依赖关系。Spring框架的核心是应用上下文(ApplicationContext),它扮演着...

    开源框架 Spring Gossip

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

    Spring MVC 基本框架

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

    spring源码经典实例

    2. **配置文件改进**:将`jdbc.properties`移动到合适的位置,如`src/main/resources`,并使用Spring的`PropertyPlaceholderConfigurer`或者`@Value`注解读取配置,以实现数据库连接参数的动态加载。 3. **使用...

    SSH框架中的多数据源配置.DOC

    在配置数据源连接信息时,我们使用了Spring框架的PropertyPlaceholderConfigurer来实现外部配置文件的注入。这种方式能够使得应用程序更加灵活和可维护。 在配置事务管理时,我们使用了Spring框架的...

    spring2.5 配置VM

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

    Spring动态加载配置文件

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

    spring工程需要的四个核心jar包之beans包

    - 在"beans"包中,`org.springframework.beans.factory.config`包下的`PropertyPlaceholderConfigurer`类用于处理占位符替换,实现环境变量或属性文件的值注入到Bean的属性中。 3. **Bean的生命周期管理** - ...

    Spring的log4j以及配置文件

    在Spring框架中,我们可以使用Spring的`PropertyPlaceholderConfigurer`来读取`log4j.properties`文件中的配置,并动态注入到应用中。这允许我们在不重启应用的情况下,通过修改外部的配置文件来调整日志级别和输出...

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

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

    图文搭建SSI(struts+spring+ibatis)框架

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:sqlMapping.properties ${jdbc.driverClassName} ${...

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

    Struts+Spring+Ibatis整合框架的搭建是一个常见的Java Web开发模式,它将Struts的MVC架构、Spring的依赖注入和事务管理以及Ibatis的数据持久层整合在一起,提供了高效且灵活的应用程序开发环境。以下是这个整合框架...

    Spring数据库连接等配置加密

    在IT行业中,尤其是在开发企业级应用时,...Spring提供了`PropertyPlaceholderConfigurer`和`EncryptablePropertyPlaceholderConfigurer`来实现这个功能。首先,我们需要创建一个解密器类,如下所示: ```xml ...

    Spring_0200_IOC_Introduction setter注入

    这一过程涉及到Spring的`PropertyPlaceholderConfigurer`、`BeanPostProcessor`、`InstantiationAwareBeanPostProcessor`等接口和类。 **工具的使用** Spring提供了多种方式来加载配置,例如XML配置文件、Java配置...

Global site tag (gtag.js) - Google Analytics