spring boot 工程有一个依赖工程xxx-common.jar, 这个xxx-common.jar有一个工具类XUtils,这个工具类会读取xxx-common.jar classpath下的一个配置文件。
本来XUtils.class.getResource("/xxxx.json").getPath();获取路径,读取这个配置文件信息。后来发现服务器上跑有问题。(我这边的打包方式是appassembler方式)。
后来记得spring-boot 源码里面有 自动装配下读取有 spring.factory的文件, 后来参考了一下源码。
关键地方 org.springframework.util.ClassUtils getDefaultClassLoader 方法;
//源码如下:
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
t