What is the preferred way of accessing settings inside an application?
Currently I have a singleton (service locator) thing going on where I get my settings via something like Services.getSettings().get("my.setting");
. Calls like these are sprinkled liberally around the code in whatever place the values of settings may be needed. It works great, but I can't help but get the feeling that it's not the "correct" way of doing this (most nagging point being the fact that everything is coupled to this global object). What would be some better (in the s开发者_运维技巧ense of "best practices") ways of accessing settings throughout an application? In case it helps, I'm not talking about web applications. I'm talking about standalone apps, be they desktop of server side. Ohh, and it's in Java, since I know you're going to ask... It shouldn't matter, though, because I'm looking for concepts rather than specific implementations.
for java Desktop apps Use
- Preference API
Or use following file to hold settings at some platform independent path (${user.home}
)
Properties file
xml file
In the past I have externalized settings for various classes in my (java) application in an XML file, and I used an "Inversion of Control (IoC) Container" (Spring Framework) to inject them. I find this approach very useful, because it helps in externalizing object dependencies too.
If using dependency injection, you can inject the values in the target objects. Otherwise this is a fine way to do it.
If the value of my.setting
is going to remain constant as long as the application is running, I would want to store the value in a constant.
private static final String MY_SETTING =
Services.getSettings().get("my.setting");
And use MY_SETTING
everywhere in that class. You might want to change the access modifier accordingly.
theres always
Plain Java Properties and for locale specific settings there's ResourceBundle
these can also be injected by using some DI Framework like Spring/Guice et al.
here re some tutorials and examples
- http://download.oracle.com/javase/tutorial/i18n/resbundle/concept.html
- http://download.oracle.com/javase/tutorial/i18n/resbundle/propfile.html
- http://technologiquepanorama.wordpress.com/2009/03/17/how_to_access_properties_file_in-spring/
- http://www.java2s.com/Code/Java/Spring/Spring-Properties.htm
hope that helped
In Java, there's the Preferences API. That's essentially a standardized version of your settings service, which loads the configuration from various sensible places. That might be an improvement over how you do things at the moment, but it doesn't really change the architecture, which seems to be what you're dissatisfied with.
In architectural terms, what you might be after is dependency injection; that's a topic about which so much has been written that it seems futile for me to describe it. The idea would be that some external force takes on the job of loading settings and handing them to your classes, rather than your code having to reach out and pull them in. Dependency injection is usually associated with web apps, but it's perfectly possible to use it in standalone apps. I think it's even pretty simple with PicoContainer; I have no idea about Spring.
精彩评论