My application is configured by some Spring XML configuration files.
Is it good practice in Java to include the XML files in the classpath, and reference them in my application using the classpath?
My application is configured by some Spring XML configuration files.
Is it good practice in Java to include the XML files in the classpath, and reference them in my application using the classpath?
It's nice to be able to configure it both ways - either directly as a file, or via a resource on the classpath which may or may not be in a jar file. That gives you a lot of flexibility.
I agree with the previous poster (+1) - have an option in case some day you will need it. However, there is one catch. You could create yourself lots of headache if such powerful tooling will land in the wrong hands. There is no much difference between spring context files and java classes, it's a code. So, check out who are your users.. One over enthusiastic QA guru could make your life miserable if you're not prepared.
I usually look in a system property first then the classpath. so:
java -DconfigFile=/filelocation/file.xml
can be read as:
String propfile = System.getProperty(configFile);
if (propfile != null) {
// read in file
new File(propfile);
...
} else {
// read in file from classpath
getClass.getResource("/configfile.xml")
}
I tend to put XML config files into the classpath and expose configuration that is relevant to be adapted (e.g. for different environments) into external property files using a PropertyPlaceholderConfigurer
or the like (depends on actual requirements).
A nice way to create profiles is to have properties files with a set of settings for each environment and let the administrator choose the one that is need by providing a system property whose value is then translated into a property file lookup. See API doc of PropertyPlaceholderConfigurer
for details.