java - A good way to implement Singleton pattern with Spring -


i want implement singleton pattern in context of spring application. singleton object created spring.

to that:

i put class implements applicationcontextaware beans spring context:

public class appcontext implements applicationcontextaware {    /**    * private instance of appcontext.    */   private static appcontext _instance;    /**    * @return instance of appcontext    */   public static appcontext getinstance()   {      return appcontext._instance;   }    /**    * instance of applicationcontext.    */   private applicationcontext _applicationcontext;    /**    * constructor (should never call 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;   }    /**    * instance of class define in applicationcontext.    *     * @param name_p    *          bean's identifier in applicationcontext    * @param <t>    *          type of returned bean    * @return instance of 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;   } } 

my singleton class:

    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);     }    } 

my application.xml file:

    <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> 

do think code ?

instance field no longer necessary?

considering spring manage every everything, getinstance() has return singleton instance created spring ?

i came same conclusion , similar implementation you. if needs singleton pattern way spring. wished had seen post earlier.

some people don't understand singleton pattern different singleton defined spring explained here: singleton design pattern vs singleton beans in spring container

it tricky around reflection used spring ignores private access modifier.

having said this, people hate singleton pattern can seen post: http://puredanger.github.io/tech.puredanger.com/2007/07/03/pattern-hate-singleton/

this causing me reconsider using singleton pattern design , instead use spring singleton instead.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -