spring 春季的 PropertyPlaceHolder

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8214862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:45:34  来源:igfitidea点击:

PropertyPlaceHolder in spring

springproperties

提问by Bastl

We access a java property in spring like this:

我们在 spring 中访问一个 java 属性,如下所示:

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

The bean declares

豆子声明

int fileSizeLimit = 9999;

How can I set a default if "someProperty" is missing in the properties-file?

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

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.

ATM,我们得到 NumberFormatException 因为 spring 使用属性“someProperty”的名称调用 int-setter。设置属性后,一切正常。

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

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

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

可以通过“属性”定义默认属性值,以使属性文件中的覆盖定义成为可选。如果配置器无法解析具有任何指定属性的占位符,它还将检查系统属性(例如“user.dir”)。这可以通过“systemPropertiesMode”自定义。

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

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

TIA, Bastl.

TIA,巴斯特尔。

回答by sourcedelica

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

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

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

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

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

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

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

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

The values for systemPropertiesModeName are:

systemPropertiesModeName 的值为:

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.

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

我通常在占位符中使用 SYSTEM_PROPERTIES_MODE_OVERRIDE 和默认值,所以顺序是

  1. System property
  2. Properties file
  3. Placeholder default
  1. 系统属性
  2. 属性文件
  3. 占位符默认

回答by AHungerArtist

In Spring 3 you can do ${someProperty:defaultValue}. I really hope you're not using 1.1.5.

在 Spring 3 中,您可以执行${someProperty:defaultValue}. 我真的希望你没有使用 1.1.5。

回答by Prashant Bhate

You could define default values for PropertyPlaceHolderin bean definition in xml file.

您可以PropertyPlaceHolder在 xml 文件中为in bean 定义定义默认值。

<bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
   id="corePlaceHolder">
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
            <property name="systemPropertiesModeName" 
                         value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
            <property name="searchSystemEnvironment" value="true"/>
            <property name="locations">
            <list>
                <value>classpath*:config/*/......./*.properties</value>
            </list>
            </property>
            <property name="properties">  
              <props>  
               <prop key="fileSizeLimit">123</prop>  
              </props>  
            </property>  
</bean> 

See http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.htmlfor further details

有关更多详细信息, 请参阅http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

回答by jjmontes

When using the PropertyPlaceholderConfigurer, according to its Javadoc, "the placeholder properties file is supposed to contain an entry for each defined placeholder".

当使用 PropertyPlaceholderConfigurer 时,根据其 Javadoc,“占位符属性文件应该包含每个定义的占位符的条目”。

You can, however, provide multiple locations (properties files) for your PropertyPlaceholderConfigurer, and use one of them as defaults. That way you can ensure you always have the default values you need.

但是,您可以为 PropertyPlaceholderConfigurer 提供多个位置(属性文件),并将其中之一用作默认值。这样您就可以确保您始终拥有所需的默认值。

If you want your application to throw an exception when an undefined property is used, ensure that "ignoreUnresolvablePlaceholders" is set to false in your PropertyPlaceholderConfigurer.

如果您希望您的应用程序在使用未定义属性时抛出异常,请确保在您的 PropertyPlaceholderConfigurer中将ignoreUnresolvablePlaceholders”设置为 false。