WarayWaray
WarayWaray

Reputation: 13

Spring: Injecting Properties using @Value and setting a default

So, I'm trying to set a default properties file for dbProperties. But it doesn't seem to work.

Any idea what I'm doing wrong here? Any help would be appreciated.

This is what I have in my Application.xml file:

<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
}

My end goal is to point dbProperties to defaultDbProps if dbProps is not provided.

Upvotes: 1

Views: 713

Answers (1)

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18383

Before all: defaultDbProps location is the same of dbProps one.

You are accessing properties value in the wrong way (with spEL); use ${} tag in this manner @Value('${my.property}'), but to have ${} available you need a PropertyPlaceholderConfigurer:

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

with this bean defaultDbProps are loaded first, then dbProps overrides properties with the same name

Upvotes: 1

Related Questions