有没有类似 Tomcat 的类加载器可以独立使用?
我正在使用 Java 应用程序服务器(Smartfox),它可以运行多个应用程序(“扩展”),但有一个非常不方便的类路径设置来配合它,以及尝试使用 SLF4J 时的问题。
为了解决这个问题,我想将我的应用程序包装在它们自己的类加载器中。这样的包含类加载器应该很像 Tomcat 的类加载器,因为它
- 可以从包含 JAR 的目录加载类。
- 更喜欢来自其自己的类路径的类而不是来自父级的类
是否有某个库具有这样的类加载器,我可以在我的项目中“拖放”?如果没有的话,自己创建会不会很困难?有什么已知的陷阱吗?
I'm working with a Java sort-of-application-server (Smartfox) which can run multiple applications ("extensions") but has a very inconvenient classpath setup to go along with it, along with issues when trying to use SLF4J.
To work around that I'd like to wrap my applications in their own classloaders. Such a containing classloader should be much like Tomcat's, in that it
- Can load classes from a directory containing JARs.
- Prefers classes from its own classpath over those from the parent
Is there a library somewhere that has such a classloader I can just "drag and drop" in my project? If not, would it be hard to create it myself? Any known pitfalls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OSGi(和其他模块系统)旨在准确地处理这种情况的问题。
一开始它可能看起来有点大材小用,但我认为您很快就会重新实现 OSGi 已经为您做的事情的重要部分。
例如,Equinox 是 Eclipse 使用的 OSGi 实现。
OSGi (and other module systems) are designed to handle exactly this kind of problem.
It might look like overkill at first, but I think you'll quickly re-implement significant parts of the things that OSGi alread does for you.
Equinox is the OSGi implementation used by Eclipse, for example.
由于我在嵌入 OSGi 容器时遇到了麻烦,而且确实有点矫枉过正,所以我推出了自己的解决方案。但有一天我会在不需要嵌入框架的情况下学习使用 OSGi。
如果您碰巧想使用此代码,则它处于“用它做任何您想做的事”许可之下。
Since I had trouble embedding an OSGi container and it was indeed a bit overkill, I rolled my own solution. But I'll learn to use OSGi one day, in a situation where I don't need to embed the framework.
If you somehow happen to want to use this code, it's under the "do whatever you want with it" license.