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中定义
的工具。
分享到:
相关推荐
`PropertyPlaceholderConfigurer`用于加载配置文件,`JedisPoolConfig`配置连接池参数,`JedisConnectionFactory`则用于创建与Redis的连接。 4. **Spring Bean注入**:在SSH应用的其他配置文件中,如Spring的...
### SSH框架整合详解 #### 一、概述 SSH框架整合主要指的是Struts2、Spring以及Hibernate这三个技术的结合使用,以此来构建一个高效且可扩展性强的企业级应用系统。Struts2负责处理前端请求,Spring作为核心管理...
### Spring 容器后处理器详解 #### 一、Spring 容器后处理器概念及应用场景 在Spring框架中,容器后处理器(BeanFactoryPostProcessor)是一种非常实用的设计模式,主要用于在容器初始化完成后对容器进行额外的...
Spring MVC提供了一种集中配置的方式,例如使用PropertyPlaceholderConfigurer和ContextLoaderListener等机制,来管理不同部署环境下的配置文件,这样就无需更改源代码即可切换不同的环境配置。 配置管理是任何Web...
### SSH整合详解 #### 一、概述 SSH是Struts + Spring + Hibernate三个开源框架的首字母缩写,这三个框架组合在一起,形成了一个强大的企业级应用开发架构。本篇文章将详细解析SSH集成开发环境的搭建过程,从环境...
总结来说,Spring通过`PropertyPlaceholderConfigurer`加载属性文件,并提供了两种使用方式:直接在bean配置中引用属性值,或通过注解在Java代码中注入属性。这种方式增强了代码的可维护性和灵活性,使得配置信息...
如`*:jdbc.properties">`设置了`PropertyPlaceholderConfigurer`的`locations`属性,指定了属性文件的位置。 4. `<context:property-placeholder>`:这是一个特殊的bean,用于加载外部属性文件(如`jdbc.properties...
当需要从属性文件(如`dev.properties`)中读取值时,可以使用`PropertyPlaceholderConfigurer`。首先,在Spring配置文件中定义这个bean: ```xml <bean class="org.springframework.beans.factory.config....
首先,我们需要创建一个继承自Spring的`PropertyPlaceholderConfigurer`类的自定义配置器。这个类的作用是解析配置文件中的属性,并在必要时进行解密。以下是一个示例: ```java import org.springframework.beans....
Spring 无法读取 properties 文件数据问题详解 Spring 框架在读取 properties 文件数据时可能会遇到一些问题,本文将对这些问题进行详细的解释和解决。 问题一:Controller 中无法读取 properties 文件数据 在 ...
Spring Value 注解详解 Spring Value 注解是 Spring 框架中的一种重要注解,用于从 properties 文件中获取配置值。从 Spring 3 开始,Spring 框架支持使用 @Value 注解来简化读取配置文件的操作。 配置 properties...
在Spring XML配置文件中,可以使用PropertyPlaceholderConfigurer类来加载Properties配置文件。例如: ``` <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>...
《Spring配置文件详解》 Spring框架的核心之一就是其配置文件,它是实现依赖注入(Dependency Injection,简称DI)的关键。配置文件通常以XML格式存在,用于定义bean及其相互依赖关系,从而构建应用程序的组件模型...
Spring Boot 外部配置(配置中心化)详解 Spring Boot 框架提供了多种方式来进行外部配置,其中配置中心化是其中一种重要的方式。本文将详细介绍 Spring Boot 外部配置(配置中心化)的实现方式和原理。 配置中心...
### Spring启动时根据配置文件加载不同文件的知识点详解 #### 一、背景介绍 在实际的应用开发中,根据运行环境的不同(例如开发环境、测试环境、生产环境等),应用程序往往需要连接不同的数据库或其他资源。为了...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:jdbc.properties ``` 这样,可以在Java代码或XML配置中使用`$...
在Java开发中,Hibernate是一个非常流行的ORM(对象关系映射)框架,它允许...这通常通过Spring框架来管理,例如使用`PropertyPlaceholderConfigurer`加载配置文件,然后在`DataSource`的配置中引用这些属性: ```xml ...
Spring中利用配置文件和@Value注解注入属性值代码详解 在Spring框架中,配置文件和@Value注解是两个常用的注入属性值的方法。本文将详细介绍如何使用配置文件和@Value注解注入属性值,并提供了代码示例。 一、简单...