Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:使用@Value注入属性并设置默认值_Spring_Groovy - Fatal编程技术网

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

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

因此,我试图为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
}

@配置
类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覆盖具有相同名称的属性