GeeCON Prague 2014*
The moose likes Spring and the fly likes Setting properties in my APP using Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Setting properties in my APP using Spring" Watch "Setting properties in my APP using Spring" New topic
Author

Setting properties in my APP using Spring

Mi Ku
Greenhorn

Joined: Nov 21, 2008
Posts: 11
I have been trying to use Spring as a configuration serve for my App. I have looked into using the PRopertyPlaceholder functionality. I would like to have settings for properties set inot individual classes, not the monolithic place where all the values for PropertyPlaceholder are put.

Any advice?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Mi Ku wrote:I have been trying to use Spring as a configuration serve for my App. I have looked into using the PRopertyPlaceholder functionality. I would like to have settings for properties set inot individual classes, not the monolithic place where all the values for PropertyPlaceholder are put.

Any advice?


Use the util namespace and use <util:properties id="someName" location="somelocation.properiest"/> tag,

Now you have a bean that gets created of type Properties that you can inject into you classes.

Mark


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

Joined: Nov 21, 2008
Posts: 11
Thanks.

What if I do not want to use a properties file, just a setting for a property in a bean or a set op properties in a Singleton that I can use in many classes
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

Mi Ku wrote:Thanks.

What if I do not want to use a properties file, just a setting for a property in a bean or a set op properties in a Singleton that I can use in many classes

Then why use properties? If they are hard coded in the application, you could be using Java variables/constants.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 301
Even then, you could use the Spring Expression Language to do almost anything you want. It works in either XML...



...or with the @Value annotation...



As for the "some expression", it could be something like "#{someOtherBean.someProperty}" to pull the value of some other bean property. Or it could be "#{systemProperties.SOME_SYSTEM_PROPERTY}" to pull from a system property. Or you could use "#{systemEnvironment['SOME_ENV_VARIABLE']}" to get it from an environment variable.

That only shows a tiny bit of what SpEL can do, but it seemed pertinent to the question posed here.


Spring in Action - Unleash POJO power in your applications!
Modular Java - Discover the secret weapon to modularity on the Java platform!
XDoclet in Action - Your complete guide to code generation with XDoclet.
Mi Ku
Greenhorn

Joined: Nov 21, 2008
Posts: 11
The value of the properties varies from environment to environment, hance the need to set them externally. Spring is the standard in my App. I am toying with the idea of creating a global properties file for all the settings and would like to have each individual developer 'pull' the value out of the properties file and inject them inot their beans as properties.

How would I "move" the values from the property file into each individual bean? I like the idea of the SPel, very much like the EL for JSPs

Thank you
 
GeeCON Prague 2014
 
subject: Setting properties in my APP using Spring