前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义类加载器加载jar包_类加载器的可见性

自定义类加载器加载jar包_类加载器的可见性

作者头像
全栈程序员站长
发布2022-10-04 11:03:51
7980
发布2022-10-04 11:03:51
举报

大家好,又见面了,我是你们的朋友全栈君。

spring根本不会去管自己被放在哪里,它统统使用TCCL来加载类,而TCCL默认设置为了WebAppClassLoader,也就是说哪个WebApp应用调用了spring,spring就去取该应用自己的WebAppClassLoader来加载bean。这在真正理解线程上下文类加载器(多案例分析)中已有详细描述。 因此,为了使spring使用自定义的类加载器进行加载,需要开一个线程,将这个线程的类加载器设置为自定义类加载器。

代码语言:javascript
复制
    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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月5日 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考网站
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档