title: CDI 1.1 Dispose injected resource Immediate after using
body: <p>I have a SimpleResource class</p>
  
  <pre><code>@Vetoed
  public class SimpleResource {
    private String value="TestValue";
    public void open(){
       System.out.println("Open SR method");  
   }  
   public void close(){
     System.out.println("Close SR method");
   }
   public String getValue() {
     return value;
   }  
  }
  </code></pre>
  
  <p>Then I've created a Producer Class</p>
  
  <pre><code>@Singleton
  public class EntityManagerProducer {
  
   @Produces
   @Default
   @Dependent
   public static SimpleResource createSimpleResource() {
     SimpleResource sr = new SimpleResource();
     System.out.println("Open SR");
     sr.open();
     return sr;
   }
  
   public static void disposeSimpleResource(@Disposes @Default SimpleResource  simpleResource) {
    System.out.println("Close SR");
    simpleResource.close();
    }
  }
  </code></pre>
  
  <p>And than a Singleton Business class </p>
  
  <pre><code>@Default
  @Singleton
  public class InjectConstructor {
   private  String value="init";
  
   @Inject
   public InjectConstructor(SimpleResource sr){
    value=sr.getValue();
   }
  
   public String getValue() {
     return value;
   }
  }
  </code></pre>
  
  <p>The test method to run</p>
  
  <pre><code> @Test
   public void injectConstructor(){
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    InjectConstructor inst= container.select(InjectConstructor.class).get();   
    System.out.println("Value= "+inst.getValue());
   }
  </code></pre>
  
  <p>After runnning  test program  I've got such response: </p>
  
  <p>INFO  org.jboss.weld.Bootstrap - WELD-ENV-002003: Weld SE container STATIC_INSTANCE initialized <br>
  Open SR<br>
  Open SR method<br>
  Value= TestValue<br>
  Close Container<br>
  Close SR<br>
  Close SR method<br></p>
  
  <p>The problem is that "SimpleResource" is opend before constructor @Inject  BUT not close after constructor exited.</p>
  
  <p>The question is How to inject "SimpleResource" through Constructor injection and closed Immediate after constructor exited?</p>
  
  <p>Is the only tecnique to do this is using @Observes method?</p>
  
  <pre><code>public void watchStartup(@Observes @Initialized(ApplicationScoped.class) Object ctxEvent, SimpleResource sr) {
   ...
  }
  </code></pre>