2016-07-12 15 views
0

Springアプリケーションのコンテキストでシングルトンパターンを実装したいと思います。 だから私のシングルオブジェクトも春までに作成されます。これを行うにはSpringでシングルトンパターンを実装する良い方法

私は春のコンテキストから豆を取得するためにApplicationContextAwareを実装するクラス置く:

public class AppContext implements ApplicationContextAware 
{ 

    /** 
    * Private instance of the AppContext. 
    */ 
    private static AppContext _instance; 

    /** 
    * @return the instance of AppContext 
    */ 
    public static AppContext getInstance() 
    { 

    return AppContext._instance; 
    } 

    /** 
    * Instance of the ApplicationContext. 
    */ 
    private ApplicationContext _applicationContext; 

    /** 
    * Constructor (should never be call from the code). 
    */ 
    public AppContext() 
    { 
    if (AppContext._instance != null) 
    { 
     throw (new java.lang.RuntimeException(Messages.getString("AppContext.singleton_already_exists_msg"))); //$NON-NLS-1$ 
    } 
    AppContext._instance = this; 
    } 

    /** 
    * Get an instance of a class define in the ApplicationContext. 
    * 
    * @param name_p 
    *   the Bean's identifier in the ApplicationContext 
    * @param <T> 
    *   the type of the returned bean 
    * @return An instance of the class 
    */ 
    @SuppressWarnings("unchecked") 
    public <T> T getBean(String name_p) 
    { 

    return (T) _applicationContext.getBean(name_p); 
    } 

    @Override 
    public void setApplicationContext(ApplicationContext applicationContext_p) throws BeansException 
    { 
    _applicationContext = applicationContext_p; 
    } 
} 

マイシングルトンクラス:

public class MySingleton { 

    private static MySingleton instance= null; 
    public static final String BEAN_ID = "mySingletonInstanceId"; 

    private MySingleton(){} 


    public static MySingleton getInstance(){ 
     return AppContext.getInstance().getBean(mySingletonInstanceId.BEAN_ID); 
    } 
    } 

マイapplication.xmlはファイル:

<beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 
      xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
      xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
    http://cxf.apache.org/jaxrs 
    http://cxf.apache.org/schemas/jaxrs.xsd 
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 

    <!--some code--> 

     <bean id="idAppContext" class="com.AppContext" /> 
     <bean id="mySingletonInstanceId" class="com.MySingleton"/> 

    <!--some code--> 

    </beans> 

私のコードは良いと思いますか?

フィールドは不要ですか?

Springがすべてをすべて管理することを考慮して、getInstance()は、Springで作成されたシングルトンインスタンスを返すだけですか?

+3

これを行うコードを記述しないでください。この醜い仕掛品の代わりに依存性注入を使用してください。 –

+5

デフォルトでは、Spring Beanはシングルトンです。依存性注入を使用するだけで春はあなたのためにそれを行います。 –

+0

Springアプリケーションでシングルトンパターンを使用する必要はなくなりましたか? –

答えて

1

私はあなたと同じ結論と同様の実装になりました。誰かがシングルトンパターンを必要とする場合は、これがSpringでそれを行う方法です。私はこの記事を以前に見てほしかった。 Singleton design pattern vs Singleton beans in Spring container

効果的に民間を無視春で使用される反射を回避するためにトリッキーです:

一部の人々は、Singletonパターンは、ここで説明されている春で定義されたシングルトンとは異なっていることを理解していませんアクセス修飾子。この記事で見ることができるように

はこのことを言って、何人かの人々はシングルトンパターンを憎む:http://puredanger.github.io/tech.puredanger.com/2007/07/03/pattern-hate-singleton/

これは私はシングルトンパターン設計を使用して再考し、代わりに代わりに春のシングルトンを使用することが原因となっています。

関連する問題