类加载器特定属性

发布于 2024-11-01 08:57:37 字数 320 浏览 2 评论 0原文

我们开发了一个应用程序容器,它为容器中运行的每个独立应用程序创建一个新的类加载器。当调用特定应用程序时,线程的上下文类加载器将使用应用程序的类加载器进行适当设置。

避免使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-11-08 08:57:37

您可以让它加载自定义属性类,而不是强制转换类加载器,例如

public class AppClassloaderProperties
{
   static Properties appProperties = loadAppProperties();

   static private Properties loadAppProperties() {
        // fetch app properties - does not need to be thread-safe, since each invocation
        // of this method will be on a different .class instance
   }

   static public final Properties getApplicationProperties() {
      // this method should be thread-safe, returning the immutable properties is simplest
      return new Properties(appProperteis);   
   }
}

,由于该类作为应用程序类加载器的一部分加载,因此为每个应用程序提供了一个新类。每个应用程序的 AppClassloaderProperties 类都是不同的。然后,每个应用程序都可以通过调用

Properties props = AppClassloaderProperties.getApplicationProperties();
// use the properties

No need for thread locals 或强制转换当前类加载器来获取其类加载器属性。

Rather than casting the classloader, you can have it load a custom properties class, e.g.

public class AppClassloaderProperties
{
   static Properties appProperties = loadAppProperties();

   static private Properties loadAppProperties() {
        // fetch app properties - does not need to be thread-safe, since each invocation
        // of this method will be on a different .class instance
   }

   static public final Properties getApplicationProperties() {
      // this method should be thread-safe, returning the immutable properties is simplest
      return new Properties(appProperteis);   
   }
}

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 calling

Properties props = AppClassloaderProperties.getApplicationProperties();
// use the properties

No need for thread locals or casting the current classloader.

夜灵血窟げ 2024-11-08 08:57:37

如何子类化上下文类加载器,使用所需的属性支持扩展它,然后仅强制转换 Thread.currentThread().getContextClassLoader()?

How about subclassing the context classloader, extending it with the property support you need, then just casting the Thread.currentThread().getContextClassLoader()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文