title: JAX-RS: Dependency Injection with Shutdown/Destroy/Dispose
body: <h3>Actual Problem</h3>
  
  <p>I’m trying to create a simple JAX-RS <code>Application</code> with Jersey 2.0 like this:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@ApplicationPath("")
  public class MyService extends ResourceConfig {
  
    public MyService() {
  
      packages(MyService.class.getPackage().getName());
      register(new ApplicationBinder());
    }
  
  
    private static class ApplicationBinder extends AbstractBinder {
  
      @Override
      protected void configure() {
  
        MyDependency dep = new MyDependency();
        bind(dep).to(MyDependency.class);
        // TODO properly shutdown/destroy/dispose "dep" somewhere
      }
  
    }
  
  }
  </code></pre>
  
  <p><code>MyDependency</code> objects are a little expensive to create and they have a <code>shutdown()</code> method which needs to be called before destruction. Where and how should I do such cleanup tasks with JAX-RS?</p>
  
  <h3>More Background (if Necessary)</h3>
  
  <p>The JAX-RS resource implementation which uses the created <code>MyDependency</code> object looks like this – if that should be of any relevance to the problem:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Path("getit")
  @Singleton
  public class MyResource {
  
    private final MyDependency dependency;
  
    @Inject
    public MyResource(MyDependency dependency) {
  
      this.dependency = dependency;
    }
  
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getIt() {
  
      return this.dependency.getIt();
    }
  
  }
  </code></pre>