Spring:使用@Value注入属性并设置默认值
因此,我试图为dbProperties设置一个默认属性文件。但它似乎不起作用 知道我做错了什么吗?任何帮助都将不胜感激 这是我的Application.xml文件中的内容:Spring:使用@Value注入属性并设置默认值,spring,groovy,Spring,Groovy,因此,我试图为dbProperties设置一个默认属性文件。但它似乎不起作用 知道我做错了什么吗?任何帮助都将不胜感激 这是我的Application.xml文件中的内容: <util:properties id="dbProps" location="classpath:dbConf.properties" /> <util:properties id="defaultDbProps" location="classpath:dbConf.properties" />
<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
}
@配置
类DBConfig{
@值(“#{dbProps:#{defaultDbProps}}}”)
私有属性dbProperties
}
我的最终目标是在没有提供dbProps的情况下将dbProperties指向defaultDbProps。首先:
defaultDbProps
位置与dbProps
位置相同。您以错误的方式访问属性值(使用spEL);以这种方式使用
${}
标记@Value('${my.property}')
,但要使${}
可用,您需要PropertyPlaceHolderConfigure
:<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覆盖具有相同名称的属性:
defaultDbProps
位置与dbProps
位置相同。您以错误的方式访问属性值(使用spEL);以这种方式使用
${}
标记@Value('${my.property}')
,但要使${}
可用,您需要PropertyPlaceHolderConfigure
:<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覆盖具有相同名称的属性