在Java类路径中包含XML配置的好习惯?

36 java spring

我的应用程序由一些Spring XML配置文件配置.

在Java中将XML文件包含在类路径中并使用类路径在我的应用程序中引用它们是不错的做法?

Nat*_*ger 229

我通常首先查看系统属性然后查看类路径.所以:

java -DconfigFile=/filelocation/file.xml
Run Code Online (Sandbox Code Playgroud)

可以理解为:

String propfile = System.getProperty(configFile);
if (propfile != null) {
 // read in file
  new File(propfile);
 ...
} else {
  // read in file from classpath
  getClass.getResource("/configfile.xml")
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 61

很高兴能够以两种方式配置它 - 直接作为文件,或通过类路径上的资源,可能是也可能不在jar文件中.这给了你很大的灵活性.