aspose file tools*
The moose likes Spring and the fly likes Dynamic properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Dynamic properties " Watch "Dynamic properties " New topic

Dynamic properties

Kev Adams

Joined: Oct 27, 2009
Posts: 23
Is it possible for Spring to instantiate a bean with a dynamic property?

For example, I have property files representing different environments (i.e. test and production). I want the bean to be instantiated with different property values depending on where it's running.

i.e. (for the test environment)

and for the production environment

Or, if this isn't the correct way, could someone elaborate on how you would go about doing that?
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17250

Yep, that is what the PropertyPlaceholderConfigurer class is for. If you use the context namespace in your xml config file it can be

<context:property-placeholder location=""

then in your bean definition you can have

><bean id="a" class="someImpl">
<property name="aproperty" value = "${}"/>

This reads in a proeprty file and will replace all the ${} with values in the property file that match the property name within the {}

Hope that helps point you in the right direction.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Kev Adams

Joined: Oct 27, 2009
Posts: 23
Correct me if I'm wrong, but even if I used: <context:property-placeholder location="" /> ,then I'm still specifying an exact properties file name.

This would mean that I would have to re-build my application and replace the contents of "" (for instance with a build script or something) with production properties when I deploy it to production, correct?

I was hoping to create two properties files (i.e., and and include them both in the ear, and when the app was started, and spring initialized the bean, it could programmatically determine which properties file to load based on some criteria, like server name or a jndi lookup, or whatever.

Does that make sense?

Svein Erik Løvland

Joined: Nov 13, 2009
Posts: 8

Yes, it makes sense. AFAIK there is no support for this in the springframework, yet.

I know Kåre Nilsen(who was my spring trainer) has implemented a solution to the challenge named Staged Spring, but his site is down as I am typing this.
Kev Adams

Joined: Oct 27, 2009
Posts: 23
Okay, thanks for the reply, though. I'll figure out some other way.
David Newton

Joined: Sep 29, 2008
Posts: 12617

There are a couple of trivial ways to implement this.

One is to create a class that lists the config files to load, so that in addition to the default it could, say, load a config file based on the host name. You could do the same thing with property files and a custom property placeholder configurator.

Another is to create a property placeholder configurator that looks for property names prefixed with a host name, and if it exists, use that value in place of the "bare", or unqualified, name (or names with a prefix, like this:)Both of these solutions are very, very simple to create.

Kev Adams

Joined: Oct 27, 2009
Posts: 23
Thanks, David.
Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
Another very simple alternative will be to actually use a property placeholder and set the key in the environment variable. that way you do not need to actually build any files. You can simply change the environment variable on different servers/JVM and get the job done neatly.

- Aditya Webservices Blog
jQuery in Action, 2nd edition
subject: Dynamic properties