user108210
user108210

Reputation:

Good practice to include XML config in Java 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?

Upvotes: 39

Views: 13865

Answers (4)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83051

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.

Upvotes: 2

Nathan Feger
Nathan Feger

Reputation: 19496

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

Upvotes: 236

Dima
Dima

Reputation: 4128

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.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499860

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.

Upvotes: 69

Related Questions