Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting properties in my APP using Spring

 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34375
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Walls
author
Ranch Hand
Posts: 363
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic