春季的PropertyPlaceHolder [英] PropertyPlaceHolder in spring

查看:92
本文介绍了春季的PropertyPlaceHolder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在春季这样访问java属性:

We access a java property in spring like this:

<property name="fileSizeLimit" value="${someProperty}" />

bean声明

int fileSizeLimit = 9999;

如果属性文件中缺少"someProperty",如何设置默认值?

ATM上,我们得到NumberFormatException,因为spring调用属性名称为"someProperty"的int-setter.设置该属性后,一切正常.

ATM, we get NumberFormatException because spring calls the int-setter with the name of the property "someProperty". When the property is set, everything works fine.

http://static.springsource.org/spring/docs/1.1.5/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html 说:

可以通过属性"定义默认属性值,以进行覆盖定义 在属性文件中是可选的.配置器还将检查系统属性 (例如"user.dir"),如果它不能解析任何指定的占位符 特性.可以通过"systemPropertiesMode"自定义.

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".

这是否设置了查找属性的顺序?我在哪里配置?

Does this set the order in which properties are looked up? Where do I configure this?

TIA,巴斯特尔.

推荐答案

对于第一个问题,您可以使用以下语法为占位符设置默认值,其中默认值为9999.

For your first question, you can set a default value for placeholder using the following syntax, where 9999 is the default.

<property name="fileSizeLimit" value="${someProperty:9999}" />

对于第二个问题,属性systemPropertiesModeName确定属性的解析顺序,属性文件与系统属性.例如

For your second question, the property systemPropertiesModeName determines the order in which properties are resolved, properties file versus system properties. For example,

<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>

告诉PropertyPlaceholderConfigurer在属性文件之前先查看系统属性.

Tells the PropertyPlaceholderConfigurer to look at the system property before the properties file.

systemPropertiesModeName的值是:

The values for systemPropertiesModeName are:

SYSTEM_PROPERTIES_MODE_FALLBACK (default)
      Check system properties if not resolvable in the specified properties.
SYSTEM_PROPERTIES_MODE_NEVER 
      Never check system properties.
SYSTEM_PROPERTIES_MODE_OVERRIDE 
      Check system properties first, before trying the specified properties.

我通常在占位符中使用SYSTEM_PROPERTIES_MODE_OVERRIDE和默认值,因此顺序应为

I usually use SYSTEM_PROPERTIES_MODE_OVERRIDE and default values in my placeholders, so the order would be

  1. 系统属性
  2. 属性文件
  3. 默认占位符

这篇关于春季的PropertyPlaceHolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆