6

I have a properties file defined in my xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/db.properties"></property>    
</bean>

I have a property in the file:

someprop = one

Question

In my XML I would like to either add/remove a property in a bean definition. For example:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.internal.url}" />
    <!--I want to add/remove the line below based on value in property file-->
    <property name="username" value="${jdbc.internal.username}" />
</bean>
birdy
  • 9,286
  • 24
  • 107
  • 171
  • 1
    Might I suggest having different property files for different environments instead. That is the "standard" way to solve this. – pap Nov 08 '12 at 16:32
  • that could work as well. Though, I'm confused as to how it'll be determined which one gets picked up? Do I have to configure an environment name in tomcat? Is there a link that goes in depth? – birdy Nov 08 '12 at 16:34
  • Anyway, since this is XML and not properties file. I'll still have to change the xml file when deploying from one env to the other. – birdy Nov 08 '12 at 16:37

1 Answers1

4

This might be possible using SpringEL. Read the Expression support for defining bean definitions section here.

If this is to handle multiple environments then Spring 3.1 provides bean definition profiles and Environment abstractions.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • I haven't found this answer in the links you suggested. How does the profile get trigger? Say I have two profiles, where do I make one of them active – birdy Nov 08 '12 at 17:29
  • @birdy the active profile can be set multiple ways. See this post for more info http://stackoverflow.com/questions/8587489/how-to-set-active-spring-3-1-environment-profile-via-a-properites-file-and-not-v – Aravind Yarram Nov 08 '12 at 17:34