spring属性文件配置

Spring的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类用来引入properties文件,并且可以在applectionContext.xml文件的其他部分通过 ${} 的形式引用properties文件中定义的键值对。

PropertyPlaceholderConfigurer的API说明:

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:


${driver} jdbc:${dbname}

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 convertPropertyValue method. For example, encrypted values can be detected and decrypted accordingly before processing them.

PropertyPlaceholderConfigurer类实现了BeanFactoryPostProcessor 接口。

示例:


classpath*:/application.properties

classpath*:/application.cluster.properties

classpath*:/application.local.properties

file:/var/application.server.properties

说明:

(1)systemPropertiesModeName=SYSTEM_PROPERTIES_MODE_OVERRIDE 配置表示:JVM的系统属性值会覆盖properties文件中相应的属性值。举个例子:
如果properties文件中有如下配置:


jdbc.url=aaa

同时JVM有如下的启动参数:


-Djdbc.url=bbb

那么,jdbc.url的值为bbb,而不是aaa。

(2)ignoreResourceNotFound=ture 配置表示不读取丢失的properties文件。该特性可以用来实现不同的配置策略,比如,配置开发环境和生产环境。

我们也可以使用org.springframework.beans.factory.config.PropertyOverrideConfigurer 来实现上面示例类似的功能。PropertyOverrideConfigurer类允许配置文件加载后动态的覆盖某些bean的属性,当没有显式配置的时候使用默认的配置,否则使用覆盖配置。



test.properties是一个属性文件,其格式为:bean.property=value,比如你想覆盖datasource的driverClassName属性值,需要写成dataSource.driverClassName=jdbc:mysql:…. 。而 PropertyPlaceholderConfigurer 中属性文件的key写的是占位符的变量名,注意区别。

属性集合按照如下方式配置:


beanId.propertyName1[prop1]=value
beanId.propertyName1[prop2]=value

将testPropertyConfigurer放在单独的xml文件中,如果不加载这个xml,那么dataSource.jdbcUrl为dao.properties中的值;如果加载这个文件,那么dataSource.jdbcUrl为test.properties中的值。

参考:
http://blog.csdn.net/catstiger/archive/2006/08/31/1147985.aspx

http://jeffreyhsu.iteye.com/blog/39735

http://neo.iteye.com/blog/570250

此条目发表在java/j2ee分类目录,贴了, , 标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据