This post originated from an RSS feed registered with Java Buzz
by Russell Beattie.
Original Post: ResourceBundle's vs. Loading Properties
Feed Title: Russell Beattie Notebook
Feed URL: http://www.russellbeattie.com/notebook/rss.jsp?q=java,code,mobile
Feed Description: My online notebook with thoughts, comments, links and more.
I have an Initialization Servlet I've used for quite a while now, it loads up when the web-app starts and grabs a .properties file with the arbitrary options I need to store for the application - everything from media directories to web service passwords.
Umm. Wow. That's a hell of a lot cleaner, no? And I don't need a dedicated Servlet to pull in those values. A ResourceBundle isn't a Properties class by any stretch, but for what I need it's pretty much perfect. I've played with this before but could never get it working (the ClassLoader() got me), so this is an amazing discovery.
The question is, which is the best thing to do? The ResourceBundle properties file needs to be in the CLASSPATH, which may be a GoodThing (TM) as you could dynamically swap out properties just by changing an environment variable, but is a potential source of bugs as versions pop up in .jar files you weren't expecting. The other thing is that you can't save values to a ResourceBundle - it's read only. Though I can promise you, I've never even thought about doing that in a webapp.
Or maybe there's another option? Personally the whole ResourceBundle vs Properties vs. PropertyResourceBundle vs JDK 1.4's java.util.prefs package just boggles me. One line for pulling in default property values via the ResourceBundle is mighty tempting though, I have to say, regardless of whether it's the "right" thing to do or not.
-Russ
P.S. JavaDocs.org rules! It's quickly become as frequently used while I program as Google. That's how I grabbed the urls to the classes above. Seriously, the guy who launched that site deserves a medal and a free pass to JavaOne for life.