Spring provides an embedding of configuration information found in XML files. I donβt want people installing my software to edit XML files, so to get configuration information more correctly in a text file (for example, path information), I returned to using java.util.Properties since it is simple in use and Spring is pretty good if you use ClassPathResource, which allows the location of the file to be access-free (it just needs to be in the class path, I put mine in the root of WEB-INF / classes.
Here is a quick method that returns a populated Properties object:
public Properties loadPropertiesFromClassPath( String filename ) throws IOException { Properties properties = new Properties(); if ( filename != null ) { Resource rsrc = new ClassPathResource(filename); log.info("loading properties from filename " + rsrc.getFilename() ); InputStream in = rsrc.getInputStream(); log.info( properties.size() + " properties prior to load" ); properties.load(in); log.info( properties.size() + " properties after load" ); } return properties; }
The file itself uses the usual format "name = value", but if you want to use the XML format "Properties", simply change the property .load (InputStream) to properties.loadFromXML (InputStream). Hope this helps.
Ichiro Furusato Apr 13 '10 at 11:37 2010-04-13 11:37
source share