0Day Forums
Good practice to include XML config in Java classpath? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: FrameWork (https://0day.red/Forum-FrameWork)
+---- Forum: Spring (https://0day.red/Forum-Spring)
+---- Thread: Good practice to include XML config in Java classpath? (/Thread-Good-practice-to-include-XML-config-in-Java-classpath)



Good practice to include XML config in Java classpath? - laranmielp - 08-02-2023

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?


RE: Good practice to include XML config in Java classpath? - superbitch717750 - 08-02-2023

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.


RE: Good practice to include XML config in Java classpath? - Mrstratagematically3 - 08-02-2023

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.


RE: Good practice to include XML config in Java classpath? - jaimennjbjjrg - 08-02-2023

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




RE: Good practice to include XML config in Java classpath? - svenm - 08-02-2023

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`][1] 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`][1] for details.


[1]:

[To see links please register here]