用Spring实现Singleton模式的好方法

时间:2016-07-12 11:57:27

标签: java spring design-patterns singleton

我想在Spring应用程序的上下文中实现Singleton模式。 所以即使是我的单身对象也会由spring创建。

要做到这一点:

我放了一个实现 ApplicationContextAware 的类来从spring上下文中获取bean:

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创建的单例实例?

1 个答案:

答案 0 :(得分:1)

我得出了与你相同的结论和类似的实现。如果有人需要Singleton模式,那么这就是使用Spring的方法。我希望我早些时候见过这篇文章。

有些人不明白Singleton模式与Spring定义的Singleton不同,这里解释:Singleton design pattern vs Singleton beans in Spring container

绕过Spring使用的反射是很棘手的,它有效地忽略了私有访问修饰符。

话虽如此,有些人讨厌Singleton模式,这篇文章可以看出:http://puredanger.github.io/tech.puredanger.com/2007/07/03/pattern-hate-singleton/

这让我重新考虑使用Singleton Pattern Design,而是改用Spring Singleton。