title: Correct way of making a singleton a Spring bean
body: <p>I am converting a singleton to a Spring bean, so that if the singleton fails to initialize, then entire web application's spring context doesn't load properly. </p>
  
  <p>The advantage of making the Spring context not load properly, is that people will take notice and fix the configuration during deployment itself. As opposed to using 'non-spring bean' singleton: when that throws exception during initialization, nobody notices.. until a actual user complains of missing functionality.</p>
  
  <p>My changes are working as expected.. but I am not sure if I am doing the right thing.<br>
  Any thoughts?</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>public class MySingleton {
  
      private static MySingleton INSTANCE = null;
      private MySingleton(){}
  
  
  public static MySingleton getInstance(){
      if(INSTANCE == null){
          synchronized(MySingleton.class){
              if(INSTANCE == null){
                  try{
                      doWork()
                  }catch(Exception e){
                      throw new IllegalStateException("xyz", e);
                  }
                  INSTANCE = new MySingleton();
              }
          }
      }
  
      return INSTANCE;
  }
  
  private static void doWork() {
      // do some work
      }
  
  }
  </code></pre>
  
  <p>And in the spring config xml, the bean will be defined as:</p>
  
  <pre><code>&lt;bean id="MySingletonBean"
      class="com.MySingleton"
      factory-method="getInstance" lazy-init="false" singleton="true"&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>Note:</strong>
  Most of this is similar to the strategy discussed in this article:
  <a href="http://springtips.blogspot.com/2007/06/configuration-hell-remedy-with.html" rel="noreferrer">http://springtips.blogspot.com/2007/06/configuration-hell-remedy-with.html</a></p>
  
  <p><hr>
  <strong>Edit 1:</strong></p>
  
  <p>The classes that use this singleton, are not spring beans themselves.. they are just non-spring pojos, that I can't convert to spring. They  must rely on getInstance() method get hold of the Singleton.</p>
  
  <hr>
  
  <p><strong>Edit 2:</strong> (copying a comment I made below into this description section)
   I am trying to target two things:</p>
  
  <ol>
  <li>I want Spring to initialize the singleton. So that if the
  initialization fails, then the application loading fails. </li>
  <li>I want the other classes be able to use classes without having to rely on contextAwareObj.getBean("MySingleton")</li>
  </ol>
  
  <p><hr>
  <strong>EDIT 3 (FINAL):</strong> 
  I decided to make this class a singleton.. and am not making it a spring bean.  If it fails to initialize, it will log something in the Log file.. hopefully the person doing deployment takes notice....  I abandoned the approach I mentioned earlier because I feel it will create a maintenance nightmare in future, so I had to pick between - singleton - or - spring bean. I chose singleton.</p>