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

Spring的PropertyPlaceholderConfigurer

 
阅读更多

使用PropertyPlaceholderConfigurer将系统相关变量分离懂到一个properties文件中,便于系统移植、切换。

<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<!-- 标准配置 -->
				<value>classpath*:/application.properties</value>
			</list>
		</property>
	</bean>
 

官方注释:

A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

The default placeholder syntax follows the Ant / Log4J / JSP EL style:

${...}
Example XML context definition:
<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>
Example properties file:
driver=com.mysql.jdbc.Driver
 dbname=mysql:mydb
PropertyPlaceholderConfigurer checks simple property values, lists, maps, props, and bean names in bean references. Furthermore, placeholder values can also cross-reference other placeholders, like:
rootPath=myrootdir
 subPath=${rootPath}/subdir
In contrast to PropertyOverrideConfigurer, this configurer allows to fill in explicit placeholders in context definitions. Therefore, the original definition cannot specify any default values for such bean properties, and the placeholder properties file is supposed to contain an entry for each defined placeholder.

If a configurer cannot resolve a placeholder, a BeanDefinitionStoreException will be thrown. If you want to check against multiple properties files, specify multiple resources via the "locations" setting. You can also define multiple PropertyPlaceholderConfigurers, each with its own placeholder syntax.

Default property values can be defined via "properties", to make overriding definitions in properties files optional. A configurer will also check against system properties (e.g. "user.dir") if it cannot resolve a placeholder with any of the specified properties. This can be customized via "systemPropertiesMode".

Note that the context definition is aware of being incomplete; this is immediately obvious to users when looking at the XML definition file. Hence, placeholders have to be resolved; any desired defaults have to be defined as placeholder values as well (for example in a default properties file).

Property values can be converted after reading them in, through overriding the PropertyResourceConfigurer.convertPropertyValue(java.lang.String) method. For example, encrypted values can be detected and decrypted accordingly before processing them.

 

参考:

http://thomaslee007.iteye.com/blog/167762

 

分享到:
评论

相关推荐

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

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

    Spring中PropertyPlaceholderConfigurer的使用

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

    Spring属性占位符PropertyPlaceholderConfigurer的使用

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

    Spring如何使用PropertyPlaceholderConfigurer读取文件

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

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

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

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

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

    SPRING:bean配置properties

    在Spring框架中,Bean的配置与管理是其核心功能之一,而通过`PropertyPlaceholderConfigurer`进行属性占位符的配置则是实现动态配置的关键技术。本文将深入解析如何利用`PropertyPlaceholderConfigurer`进行bean配置...

    Spring-Reference_zh_CN(Spring中文参考手册)

    PropertyPlaceholderConfigurer示例 3.7.2.2. PropertyOverrideConfigurer示例 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的...

    Spring 容器后处理器

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:application.properties ``` 在这个示例中,`...

    spring2.5 配置VM

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

    spring

    - **PropertyPlaceholderConfigurer** 和 **PropertyOverrideConfigurer**:用于在运行时动态替换Bean配置中的占位符。 ### 结论 Spring框架以其强大的功能和灵活性,成为Java企业级应用开发的首选框架之一。通过...

    spring4.0引用properties

    `PropertyPlaceholderConfigurer`是Spring早期版本中用于注入properties文件中值的bean,而`@ConfigurationProperties`是Spring Boot引入的,更适合现代Spring应用。 使用`PropertyPlaceholderConfigurer`的例子...

    Spring MVC 入门实例

    6 &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; 7 8 9 &lt;value&gt;/WEB-INF/jdbc.properties 10 11 12 13 14 它配置了以下功能: 读取...

    Spring动态加载配置文件

    `PropertyPlaceholderConfigurer`是Spring早期版本中处理属性文件的工具,而`@PropertySource`则是从Spring 3.1引入的新特性,它们都可以用来从外部属性文件中读取值并注入到bean中。 1. `...

    springmvc+spring+mybatis

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置数据源 --&gt; ${driverClassName}"/&gt; ${url}"/&gt; ${username...

    开源框架 Spring Gossip

    不使用XML定义档进行 Bean设置 Aware 相关介面 BeanPostProcessor BeanFactoryPostProcessor PropertyPlaceholderConfigurer PropertyOverrideConfigurer CustomEditorConfigurer ...

    spring读取properties

    在Spring框架中,读取和使用...在Spring的配置文件中,首先需要定义一个`PropertyPlaceholderConfigurer` bean,这是Spring用来解析Properties文件并将其值注入到其他bean中的关键组件。如示例所示: ```xml ...

    4.Spring应用扩展.pptx

    例如,可以将数据库连接信息写入属性文件,如`database.properties`,然后在Spring配置文件中使用`PropertyPlaceholderConfigurer`引入属性文件。这样,我们就可以在XML配置中使用`${...}`的方式来引用属性值,如`${...

    配置spring

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;/WEB-INF/conf/jdbc.properties &lt;!-- Define the DataSource bean --&gt; ...

Global site tag (gtag.js) - Google Analytics