• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic properties

 
Kev Adams
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="mypropertiesFile.properties"

then in your bean definition you can have

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

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.

Mark
 
Kev Adams
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I'm wrong, but even if I used: <context:property-placeholder location="mypropertiesFile.properties" /> ,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 "mypropertiesFile.properties" (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. mytestproperties.properties, and myprodproperties.properties) 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
Greenhorn
Posts: 8
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks for the reply, though. I'll figure out some other way.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, David.
 
Aditya Keyal
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic