Java 有没有办法在 Spring XML 中指定默认属性值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2513484/
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-08-13 08:41:04  来源:igfitidea点击:

Is there a way to specify a default property value in Spring XML?

javaxmlspringproperties

提问by Rog

We are using a PropertyPlaceholderConfigurerto use java properties in our Spring configuration (details here)

我们正在使用PropertyPlaceholderConfigurer在我们的 Spring 配置中使用 java 属性(详细信息在这里

eg:

例如:

<foo name="port">
  <value>${my.server.port}</value>
</foo>

We would like to add an additional property, but have a distributed system where existing instances could all use a default value. Is there a way to avoid updating all of our properties files, by indicating a default value in the Spring config for when there isn't an overriding property value defined?

我们想添加一个额外的属性,但有一个分布式系统,其中现有的实例都可以使用默认值。有没有办法通过在 Spring 配置中指示未定义覆盖属性值时的默认值来避免更新我们所有的属性文件?

采纳答案by JoseK

Are you looking for the PropertyOverrideConfigurer documented here

您是否正在寻找此处记录的 PropertyOverrideConfigurer

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-overrideconfigurer

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-overrideconfigurer

The PropertyOverrideConfigurer, another bean factory post-processor, is similar to the PropertyPlaceholderConfigurer, but in contrast to the latter, the original definitions can have default values or no values at all for bean properties. If an overriding Properties file does not have an entry for a certain bean property, the default context definition is used.

PropertyOverrideConfigurer 是另一个 bean 工厂后处理器,它类似于 PropertyPlaceholderConfigurer,但与后者相反,原始定义可以为 bean 属性设置默认值或根本没有值。如果覆盖的属性文件没有特定 bean 属性的条目,则使用默认上下文定义。

回答by lexicore

Spring 3 supports ${my.server.port:defaultValue}syntax.

Spring 3 支持${my.server.port:defaultValue}语法。

回答by Robert Tupelo-Schneck

http://thiamteck.blogspot.com/2008/04/spring-propertyplaceholderconfigurer.htmlpoints out that "local properties" defined on the bean itself will be considered defaults to be overridden by values read from files:

http://thiamteck.blogspot.com/2008/04/spring-propertyplaceholderconfigurer.html指出在 bean 本身上定义的“本地属性”将被视为默认值,会被从文件中读取的值覆盖:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  <property name="location"><value>my_config.properties</value></property>  
  <property name="properties">  
    <props>  
      <prop key="entry.1">123</prop>  
    </props>  
  </property>  
</bean> 

回答by Uberto

<foo name="port">
   <value>${my.server.port:8088}</value>
</foo>

should work for you to have 8088 as default port

应该适合您将 8088 作为默认端口

See also: http://blog.callistaenterprise.se/2011/11/17/configure-your-spring-web-application/

另见:http: //blog.callistaenterprise.se/2011/11/17/configure-your-spring-web-application/

回答by Michael B?ckling

There is a little known feature, which makes this even better. You can use a configurable default value instead of a hard-coded one, here is an example:

有一个鲜为人知的功能,这使它变得更好。您可以使用可配置的默认值而不是硬编码的值,这是一个示例:

config.properties:

config.properties:

timeout.default=30
timeout.myBean=60

context.xml:

上下文.xml:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>config.properties</value>
    </property>
</bean>

<bean id="myBean" class="Test">
    <property name="timeout" value="${timeout.myBean:${timeout.default}}" />
</bean>

To use the default while still being able to easily override later, do this in config.properties:

要使用默认值同时仍然能够在以后轻松覆盖,请在 config.properties 中执行此操作:

timeout.myBean = ${timeout.default}

回答by ryenus

The default value can be followed with a :after the property key, e.g.

默认值可以跟:在属性键之后,例如

<property name="port" value="${my.server.port:8080}" />

Or in java code:

或者在java代码中:

@Value("${my.server.port:8080}")
private String myServerPort;

See:

看:

BTW, the Elvis Operatoris only available within Spring Expression Language (SpEL),
e.g.: https://stackoverflow.com/a/37706167/537554

顺便说一句,Elvis Operator仅在 Spring Expression Language (SpEL) 中可用,
例如:https: //stackoverflow.com/a/37706167/537554

回答by Ilya

Also i find another solution which work for me. In our legacy spring project we use this method for give our users possibilities to use this own configurations:

我也找到了另一个对我有用的解决方案。在我们遗留的 spring 项目中,我们使用这种方法让我们的用户可以使用自己的配置:

<bean id="appUserProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="false"/>
    <property name="locations">
        <list>
            <value>file:./conf/user.properties</value>
        </list>
    </property>
</bean>

And in our code to access this properties need write something like that:

在我们的代码中访问这个属性需要写这样的东西:

@Value("#{appUserProperties.userProperty}")
private String userProperty

And if a situation arises when you need to add a new property but right now you don't want to add it in production user config it very fast become a hell when you need to patch all your test contexts or your application will be fail on startup.

如果出现需要添加新属性但现在您不想将其添加到生产用户配置中的情况,当您需要修补所有测试上下文或应用程序将失败时,它很快就会变成地狱启动。

To handle this problem you can use the next syntax to add a default value:

要处理此问题,您可以使用下一个语法添加默认值:

@Value("#{appUserProperties.get('userProperty')?:'default value'}")
private String userProperty

It was a real discovery for me.

这对我来说是一个真正的发现。