0

I would like to inject Spring environment-based values (such as URL's for dev, stage, prod) into my Spring xml, based on Maven profiles.

I see a few related questions like this on DI using Maven and this on switching environments but I am not seeing a specific example of how to inject an environment-based value based on the Maven profile.

Can someone please guide me.

Community
  • 1
  • 1
vinnygray
  • 171
  • 1
  • 6

1 Answers1

1

It sounds like you just need to use PropertyPlaceholderConfigurer:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<bean id="greetings" class="Greeting">
    <property name="message" value="${greeting}" />
</bean>

Which would inject the greeting system property into your bean. Then your Maven profiles can define system properties which would be injected.

Depending on what version of Spring you're using (although it sounds like an older one, given your XML bean definitions), I would be tempted to use the Maven profile to select a Spring profile. For example, you can define -Dspring.profiles.active=dev and put your config in application-dev.properties.

Steve
  • 9,270
  • 5
  • 47
  • 61
  • I am already using PropertyPlaceholderConfigurer in this application for a different reason, but I am not sure what it is adding to the equation for the solution you provide. I mean, it sounds like even without the PropertyPlaceholderConfigurer, your solution would work! – vinnygray Mar 30 '15 at 19:50
  • Having the `PropertyPlaceHolderConfigurer` means that environment properties are injected in XML (and in Java) using the `${env.var.name}` syntax. – Steve Mar 31 '15 at 08:00