spring根本不会去管自己被放在哪里,它统统使用TCCL来加载类,而TCCL默认设置为了WebAppClassLoader,也就是说哪个WebApp应用调用了spring,spring就去取该应用自己的WebAppClassLoader来加载bean。这在真正理解线程上下文类加载器(多案例分析)中已有详细描述。 因此,为了使spring使用自定义的类加载器进行加载,需要开一个线程,将这个线程的类加载器设置为自定义类加载器。
public String test(){
try{
// MyClassLoaderCustom 见 https://blog.csdn.net/qq_32506245/article/details/113059028
MyClassLoaderCustom myClassLoaderCustom = new MyClassLoaderCustom(Thread.currentThread().getContextClassLoader().getParent());
Callable<String> callable=()->{
ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");
TestA testA=(TestA) ctx.getBean("com.example.TestA");
return testA.getClass().getClassLoader().toString();
};
FutureTask<String> task=new FutureTask<>(callable);
Thread thread=new Thread(task);
// 设置线程上下文类加载器
thread.setContextClassLoader(myClassLoaderCustom);
thread.start();
while (!task.isDone());
return task.get();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
真正理解线程上下文类加载器(多案例分析) saturn java 热加载(二)资源文件 spring & logback
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。