Spring:使用@Value注入属性并设置默认值

时间:2013-08-08 21:42:34

标签: spring groovy

所以,我正在尝试为dbProperties设置默认属性文件。但它似乎没有用。

知道我在这里做错了吗?任何帮助将不胜感激。

这就是我在Application.xml文件中的内容:

<util:properties id="dbProps" location="classpath:dbConf.properties" />
<util:properties id="defaultDbProps" location="classpath:dbConf.properties" />

@Configuration
class DBConfig {
   @Value('#{dbProps:#{defaultDbProps}}')
   private Properties dbProperties
}

如果没有提供dbProps,我的最终目标是将dbProperties指向defaultDbProps。

1 个答案:

答案 0 :(得分:1)

所有:defaultDbProps位置与dbProps一位相同。

您正在以错误的方式访问属性值(使用spEL);以这种方式${}使用@Value('${my.property}')标记,但要${}可用,则需要PropertyPlaceholderConfigurer

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="propertiesArray">
    <list>
      <ref bean="defaultDbProps"/>
      <ref bean="dbProps"/>
    </list>
  </property>
</bean>

首先加载此bean defaultDbProps,然后dbProps覆盖具有相同名称的属性

相关问题