• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[SPRING] Using .properties File

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic