`
胡小米
  • 浏览: 77316 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

PropertyPlaceholderConfigurer详解(转)

 
阅读更多
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中定义
的工具。
分享到:
评论

相关推荐

    详解SSH框架和Redis的整合

    `PropertyPlaceholderConfigurer`用于加载配置文件,`JedisPoolConfig`配置连接池参数,`JedisConnectionFactory`则用于创建与Redis的连接。 4. **Spring Bean注入**:在SSH应用的其他配置文件中,如Spring的...

    Spring 容器后处理器

    ### Spring 容器后处理器详解 #### 一、Spring 容器后处理器概念及应用场景 在Spring框架中,容器后处理器(BeanFactoryPostProcessor)是一种非常实用的设计模式,主要用于在容器初始化完成后对容器进行额外的...

    Spring_MVC详解学习.pdf

    Spring MVC提供了一种集中配置的方式,例如使用PropertyPlaceholderConfigurer和ContextLoaderListener等机制,来管理不同部署环境下的配置文件,这样就无需更改源代码即可切换不同的环境配置。 配置管理是任何Web...

    Spring中属性文件properties的读取与使用详解

    总结来说,Spring通过`PropertyPlaceholderConfigurer`加载属性文件,并提供了两种使用方式:直接在bean配置中引用属性值,或通过注解在Java代码中注入属性。这种方式增强了代码的可维护性和灵活性,使得配置信息...

    详解spring applicationContext.xml 配置文件

    如`*:jdbc.properties"&gt;`设置了`PropertyPlaceholderConfigurer`的`locations`属性,指定了属性文件的位置。 4. `&lt;context:property-placeholder&gt;`:这是一个特殊的bean,用于加载外部属性文件(如`jdbc.properties...

    详解Spring通过@Value注解注入属性的几种方式

    当需要从属性文件(如`dev.properties`)中读取值时,可以使用`PropertyPlaceholderConfigurer`。首先,在Spring配置文件中定义这个bean: ```xml &lt;bean class="org.springframework.beans.factory.config....

    Spring加载加密的配置文件详解

    首先,我们需要创建一个继承自Spring的`PropertyPlaceholderConfigurer`类的自定义配置器。这个类的作用是解析配置文件中的属性,并在必要时进行解密。以下是一个示例: ```java import org.springframework.beans....

    spring无法读取properties文件数据问题详解

    Spring 无法读取 properties 文件数据问题详解 Spring 框架在读取 properties 文件数据时可能会遇到一些问题,本文将对这些问题进行详细的解释和解决。 问题一:Controller 中无法读取 properties 文件数据 在 ...

    SpringValue注解

    Spring Value 注解详解 Spring Value 注解是 Spring 框架中的一种重要注解,用于从 properties 文件中获取配置值。从 Spring 3 开始,Spring 框架支持使用 @Value 注解来简化读取配置文件的操作。 配置 properties...

    详解Spring加载Properties配置文件的四种方式

    在Spring XML配置文件中,可以使用PropertyPlaceholderConfigurer类来加载Properties配置文件。例如: ``` &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;...

    spring配置文件

    《Spring配置文件详解》 Spring框架的核心之一就是其配置文件,它是实现依赖注入(Dependency Injection,简称DI)的关键。配置文件通常以XML格式存在,用于定义bean及其相互依赖关系,从而构建应用程序的组件模型...

    Spring boot外部配置(配置中心化)详解

    Spring Boot 外部配置(配置中心化)详解 Spring Boot 框架提供了多种方式来进行外部配置,其中配置中心化是其中一种重要的方式。本文将详细介绍 Spring Boot 外部配置(配置中心化)的实现方式和原理。 配置中心...

    spring 启动时加载不同的文件

    ### Spring启动时根据配置文件加载不同文件的知识点详解 #### 一、背景介绍 在实际的应用开发中,根据运行环境的不同(例如开发环境、测试环境、生产环境等),应用程序往往需要连接不同的数据库或其他资源。为了...

    详解五种方式让你在java中读取properties文件内容不再是难题

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties ``` 这样,可以在Java代码或XML配置中使用`$...

    详解hibernate自动创建表的配置

    在Java开发中,Hibernate是一个非常流行的ORM(对象关系映射)框架,它允许...这通常通过Spring框架来管理,例如使用`PropertyPlaceholderConfigurer`加载配置文件,然后在`DataSource`的配置中引用这些属性: ```xml ...

    Spring中利用配置文件和@value注入属性值代码详解

    Spring中利用配置文件和@Value注解注入属性值代码详解 在Spring框架中,配置文件和@Value注解是两个常用的注入属性值的方法。本文将详细介绍如何使用配置文件和@Value注解注入属性值,并提供了代码示例。 一、简单...

Global site tag (gtag.js) - Google Analytics