title: JAX-RS dependency injection
body: <p>I've done projects using Spring Rest. Now we have a small rest project and planning to do with Jersey JAX-RS. I'm new to this and referred SO and other blogs to successfully implement a Rest api with dependency injection.</p>
  
  <p>Have following code.</p>
  
  <p><strong>AppConfig.java</strong></p>
  
  <pre><code>import javax.ws.rs.ApplicationPath;
  import javax.ws.rs.core.Application;
  
  @ApplicationPath("/")
  public class AppConfig extends Application {    
      @Override
      public Set&lt;Class&lt;?&gt;&gt; getClasses() {
          System.out.println("AppConfig");
          final Set&lt;Class&lt;?&gt;&gt; s = new HashSet&lt;Class&lt;?&gt;&gt;();
          s.add(Controller.class);
          s.add(AppFeature.class);
          return s;
      }
  }
  </code></pre>
  
  <p><strong>AppBinder.java</strong></p>
  
  <pre><code>import org.glassfish.hk2.utilities.binding.AbstractBinder;
  
  public class AppBinder extends AbstractBinder {
      @Override
      protected void configure() {
          System.out.println("AppBinder");
          bind(ReflectionService.class).to(ReflectionService.class);
      }
  }
  </code></pre>
  
  <p><strong>AppFeature.java</strong></p>
  
  <pre><code>import javax.ws.rs.core.Feature;
  import javax.ws.rs.core.FeatureContext;
  
  public class AppFeature implements Feature {
      @Override
      public boolean configure(FeatureContext context) {
          System.out.println("AppFeature");
          context.register(new AppBinder());
          return true;
      }
  }
  </code></pre>
  
  <p><strong>Controller.java</strong></p>
  
  <pre><code>@Path("/")
  public class Controller {   
      @Inject
      Service service;    
      public Controller(){
          System.out.println("Controller created");
      }
      // other methods
  }
  </code></pre>
  
  <p><strong>Service.java</strong></p>
  
  <pre><code>@Singleton
  public class Service    
      public Service(){
          System.out.println("Service instance created");
      }
      // other methods
  }
  </code></pre>
  
  <p>I assume that each instance of Controller and Service is created on <strong>Tomcat 8</strong> server startup and dependency injection is done. But during startup, got this on console</p>
  
  <blockquote>
    <p>INFO: Registering the Jersey servlet application, named
    com.sample.auto2.AppConfig, at the servlet mapping /*, with the
    Application class of the same name.</p>
    
    <p>AppConfig</p>
    
    <p>AppConfig</p>
    
    <p>Nov 15, 2016 12:22:20 PM org.glassfish.jersey.server.ApplicationHandler initialize
    INFO: Initiating Jersey application, version Jersey: 2.6 2014-02-18
    21:52:53...</p>
    
    <p>AppFeature</p>
    
    <p>AppBinder</p>
    
    <p>Nov 15, 2016 12:22:21 PM
    org.apache.catalina.startup.HostConfig deployDirectory</p>
  </blockquote>
  
  <p><strong>Each time</strong>, we send a request, got following in console</p>
  
  <blockquote>
    <p>Service instance created</p>
    
    <p>Controller created</p>
  </blockquote>
  
  <p>My Questions</p>
  
  <ol>
  <li>Service,Controller constructors is being called whenever we send an
  http request; does it create instances in each request or just
  calling constructor? </li>
  <li>Why System.out in <em>AppConfig</em> is called twice? </li>
  <li>Is there a better way for setting up my small project, which does not have any db access and only three post endpoints?</li>
  </ol>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As per the links provided by @Harikrishnan, added <code>@Singleton</code> for <code>Controller</code> class. Now the constructors called only once (<strong>at the very first request</strong> - Why not during server startup!!).</p>
  
  <p>But why Service class constructor called on each request (earlier before adding @Singleton to Controller), even if its singleton? Also the other problems remain.</p>
  
  <p><strong>EDIT 2</strong>:</p>
  
  <p>Thanks @peeskillet. So these are the results for me.</p>
  
  <ol>
  <li><p>This called constructors only once at very first request</p>
  
  <pre><code>bind(ReflectionService.class).to(ReflectionService.class).in(Singleton.class);
  bind(Controller.class).to(Controller.class).in(Singleton.class);
  </code></pre></li>
  <li><p>This give the error on http request</p>
  
  <pre><code>bind(ReflectionService.class).to(ReflectionService.class).in(Immediate.class);
  
  java.lang.IllegalStateException: Could not find an active context for org.glassfish.hk2.api.Immediate
  </code></pre>
  
  <p>But it's ok, because</p></li>
  <li><p>This called constructor on server startup and only once</p>
  
  <pre><code>bind(new ReflectionService()).to(ReflectionService.class);
  bind(new Controller()).to(Controller.class);
  </code></pre></li>
  <li><p>By this, injection done on startup, but 404 error on http request. (Thought Controller is configured in AppBinder, then why in AppConfig)</p>
  
  <pre><code>@ApplicationPath("/")
  public class AppConfig extends ResourceConfig  {    
      public AppConfig() {
          register(new AppBinder());
      }
  }
  </code></pre></li>
  <li><p>And this make it run, as you said!</p>
  
  <pre><code>@ApplicationPath("/")
  public class AppConfig extends ResourceConfig  {        
      public AppConfig() {
          register(new AppBinder());
          register(Controller.class);
      }
  }
  </code></pre></li>
  </ol>
  
  <p>FINALLY THESE ARE ALL I NEEDED</p>
  
  <pre><code>public class AppBinder extends AbstractBinder {
      @Override
      protected void configure() {
          bind(new ReflectionService()).to(ReflectionService.class);
          bind(new Controller()).to(Controller.class);
      }
  }
  
  @ApplicationPath("/")
  public class AppConfig extends ResourceConfig  {    
      public AppConfig() {
          register(new AppBinder());
          register(Controller.class);
      }
  }
  </code></pre>