2011-04-12 9 views
0

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

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

Thread.currentThread().getContextClassLoader() 

が可能これです:

例えば、私は何とかコンテキストクラスローダにアクセスする際にプロパティを取得後に保存してできるようにしたいですか?または、ThreadLocalは唯一実行可能なオプションですか?

答えて

0

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

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 

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

関連する問題