クラスローダーをキャストするのではなく、カスタム プロパティ クラスをロードすることができます。
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
スレッド ローカルや現在のクラスローダーのキャストは必要ありません。