类加载器特定属性
我们开发了一个应用程序容器,它为容器中运行的每个独立应用程序创建一个新的类加载器。当调用特定应用程序时,线程的上下文类加载器将使用应用程序的类加载器进行适当设置。
避免使用 ThreadLocal,是否可以将属性存储在类加载器中,这样您就可以直接从类加载器中检索特定于应用程序的属性(在这种情况下)。
例如,我希望能够以某种方式保存,然后在访问上下文类加载器时检索属性:
Thread.currentThread().getContextClassLoader()
这可能吗?或者 ThreadLocal 是唯一可行的选择吗?
We developed an application container that creates a new classloader for each independent application running in the container. When a specific application is invoked, the Thread's context classloader is set appropriately with the application's classloader.
Avoiding the use of ThreadLocal, is it possible to store properties within a classloader, such that you would be able to retrieve, in this case, application-specific properties directly from the classloader.
For example, I want to be able to somehow save and then later retrieve properties when accessing the context classloader:
Thread.currentThread().getContextClassLoader()
Is this possible? Or is ThreadLocal the only viable option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让它加载自定义属性类,而不是强制转换类加载器,例如
,由于该类作为应用程序类加载器的一部分加载,因此为每个应用程序提供了一个新类。每个应用程序的
AppClassloaderProperties
类都是不同的。然后,每个应用程序都可以通过调用No need for thread locals 或强制转换当前类加载器来获取其类加载器属性。
Rather than casting the classloader, you can have it load a custom properties class, e.g.
Since this class is loaded as part of the application's classloader, a new class is provided for each application. The
AppClassloaderProperties
class for each application will be distinct. Each application can then get its classloader properties by callingNo need for thread locals or casting the current classloader.
如何子类化上下文类加载器,使用所需的属性支持扩展它,然后仅强制转换 Thread.currentThread().getContextClassLoader()?
How about subclassing the context classloader, extending it with the property support you need, then just casting the Thread.currentThread().getContextClassLoader()?