类加载器的特定属性

| 我们开发了一个应用程序容器,可以为容器中运行的每个独立应用程序创建一个新的类加载器。调用特定应用程序时,将使用该应用程序的类加载器适当地设置线程的上下文类加载器。 避免使用ThreadLocal,可以将属性存储在类加载器中,这样,在这种情况下,您就可以直接从类加载器检索特定于应用程序的属性。 例如,我希望能够以某种方式保存,然后在访问上下文类加载器时检索属性:
Thread.currentThread().getContextClassLoader()
这可能吗?还是ThreadLocal是唯一可行的选择?     
已邀请:
您可以让它加载自定义属性类,而不是强制转换类加载器,例如
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);   
   }
}
由于此类是作为应用程序的类加载器的一部分加载的,因此将为每个应用程序提供一个新的类。每个应用程序的“ 2”类将有所不同。然后,每个应用程序可以通过调用以下命令获取其类加载器属性
Properties props = AppClassloaderProperties.getApplicationProperties();
// use the properties
不需要线程局部变量或铸造当前的类加载器。     
如何对上下文类加载器进行子类化,使用所需的属性支持对其进行扩展,然后仅强制转换Thread.currentThread()。getContextClassLoader()?     

要回复问题请先登录注册