0

コンテナ内で実行される独立したアプリケーションごとに新しいクラスローダーを作成するアプリケーション コンテナを開発しました。特定のアプリケーションが呼び出されると、スレッドのコンテキスト クラスローダーがアプリケーションのクラスローダーで適切に設定されます。

ThreadLocal の使用を回避すると、クラスローダー内にプロパティを格納できますか?この場合、アプリケーション固有のプロパティをクラスローダーから直接取得できます。

たとえば、コンテキストクラスローダーにアクセスするときに、何らかの方法でプロパティを保存してから後で取得できるようにしたいと考えています。

Thread.currentThread().getContextClassLoader()

これは可能ですか?それとも、ThreadLocal が唯一の実行可能なオプションですか?

4

2 に答える 2

1

クラスローダーをキャストするのではなく、カスタム プロパティ クラスをロードすることができます。

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

スレッド ローカルや現在のクラスローダーのキャストは必要ありません。

于 2011-04-12T19:42:19.657 に答える
0

コンテキスト クラスローダーをサブクラス化し、必要なプロパティ サポートで拡張し、Thread.currentThread().getContextClassLoader() をキャストするだけではどうですか?

于 2011-04-12T19:34:44.223 に答える