简体   繁体   English

有没有一种方法可以在Spring XML中指定默认属性值?

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

We are using a PropertyPlaceholderConfigurer to 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配置中指定默认值来避免更新所有属性文件?

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

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: context.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}
<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/

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. 另一个bean工厂后处理程序PropertyOverrideConfigurer与PropertyPlaceholderConfigurer相似,但是与后者相反,原始定义可以具有默认值,也可以完全没有bean属性的值。 If an overriding Properties file does not have an entry for a certain bean property, the default context definition is used. 如果覆盖的属性文件没有某个bean属性的条目,则使用默认的上下文定义。

http://thiamteck.blogspot.com/2008/04/spring-propertyplaceholderconfigurer.html points 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> 

The default value can be followed with a : after the property key, eg 默认值后可以在属性键后跟一个: ,例如

<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 Operator is only available within Spring Expression Language (SpEL), 顺便说一句, 猫王运算符仅在Spring Expression Language(SpEL)中可用,
eg: https://stackoverflow.com/a/37706167/537554 例如: https//stackoverflow.com/a/37706167/537554

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. 对我来说,这是一个真正的发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM