title: @Singleton or getSingletons() while implementing Singleton Resources in Jersey
body: <p>I have a resource to be exposed as Restful WS.If I have to make it Singleton, what is the preferred and advised way:</p>
  
  <p>1 . Annotating the resource class using <code>@Singleton</code></p>
  
  <p>Or </p>
  
  <p>2 . Implementing the <code>getSingletons()</code> method in my Application class implementation and instantiating the resource there like</p>
  
  <pre><code>   public class RestApplication extends Application {
  
      private Set&lt;Object&gt; singletons = new HashSet&lt;Object&gt;();
  
      public RestApplication() {
          singletons.add(new PlayerResource());
      }
  
      @Override
      public Set&lt;Class&lt;?&gt;&gt; getClasses() {
          return null;
      }
  
      @Override
      public Set&lt;Object&gt; getSingletons() {
          return singletons;
      }
  }
  </code></pre>
  
  <p>I tried both ways and realised that both of them creates a singleton instance of the resource class, PlayerResource in this case.</p>