aspose file tools*
The moose likes Other Application Frameworks and the fly likes [SPRING] Using .properties File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "[SPRING] Using .properties File" Watch "[SPRING] Using .properties File" New topic
Author

[SPRING] Using .properties File

Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
This might be a very basic question for the SPRING gurus.

In my code I'm having to refer to an extenal URL. This URL is different in the development environment, testing environment and production environment. So, of course, I can't hard code the URL.

My application is built and deployed by Bamboo server. How do I use .properties file in my code so that I can refer to the urls appropriate for each environment.

Any snippets/pointers/references is highly appreciated.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Yep it is as simple as creating a PropertyPlaceholderCOnfigurer as a bean.

http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

especially using the context namespace to make it less typing.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
Hi Mark,

Thanks for the response. I looked at the documentation. One thing I could not understand. How do the values change, based on the environment? There is just one properties file. And as shown in that documentation:


The actual values come from another file in the standard Java Properties format:


The properties file can have only one "jdbc.username" for example. Now if this username is different for each environment, how do we mention those values and how would spring pick up the right value based on the environment?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

These aren't really part of Spring - it's really just part of the build process. So it depends on how Bamboo is building your project.

The way I've seen it done before in ANT scripts is to have a separate property file for each different configuration, name each one for the configuration, pass the configuration name into the build script and have the specified "<configurationName>.properties" renamed to the expected "jdbc.properties".

I've also seen properties files divided into sections and all the values added into the properties file and just commenting out the unused parts (using # in front of the line) - manual process, and prone to accidentally forgetting to return the properties to previous configurations - so not recommended.

The way to do this through Maven would be to have different profiles with the different properties specified.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
Hi Nate,

The Bamboo server in my case uses Maven to build. Any pointers or references to that? I was also a little confused about what role applicationContext.xml plays here where Maven is involved.

Thanks,
Dibyendu
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Dibyendu Sarkar wrote:Hi Nate,

The Bamboo server in my case uses Maven to build. Any pointers or references to that? I was also a little confused about what role applicationContext.xml plays here where Maven is involved.

Thanks,
Dibyendu


The role would be that Maven, based on whichever profile you are using to run, would choose a particular applicationContext.xml which had the specific environment you wanted to use. So maven just chooses which file to add to your app.



Mark
Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
Thanks Mark!

Ah, that makes sense. Any pointers to any documentation/reference to how to use applicationContext and .properties to achieve what I'm trying to do?
I've been at it since yesterday but only finding very vague documentation here and there and can't seem to piece them together.

Thanks,
Dibyendu
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I do it a little bit differently so that the build process isn't as responsible for as much. I use a PropertyPlacehoderConfigurator but it is defined like this:



In my classpath I have the following:

/config/local
/config/test
/config/stage
/config/prod

Each folder contains a database.properties file with that environments specific details. Setting the ${environment} value is as simple as:

environment=local
environment=test
....

I don't use maven. I use Ant to start and stop Tomcat and I just set the environment variable in the task that kicks off Tomcat. You could also set it on the OS itself in a start up script like .bash_rc, .profile, .bash_profile, <insert windows specific thing here>. Its just an environment variable, like JAVA_HOME or PATH. It just needs to be set.


GenRocket - Experts at Building Test Data
Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
I'm getting the picture a bit now. I'll try this out and will be back to you Gurus if I get stuck again or if I manage to do it.

Thank you all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [SPRING] Using .properties File