Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good practice to include XML config in Java classpath?

Tags:

java

spring

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")
}

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 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.