3 Replies Latest reply on Jan 8, 2015 10:02 AM by Jozef Hartinger

    Can you inject transient references into a method on a Servlet?

    Benjamin Confino Newbie

      I'm getting some odd behaviour and I would like to know if this is a bug or if this is correct.

       

      I have a dependent scoped bean, this bean has a method annotated with @PreDestroy that prints out a message.

       

      @Dependent

      public class TransiantDependentScopedBean {

       

       

          public void doSomething() {

              int i = 1;

              i++;

          }


       

       

          @PreDestroy

          public void preDestroy() {

              System.out.println("destroy");

          }

      }

       

      I inject that bean into a method, like this:

       

      @Inject

          public void transientVisit(@TransientReference TransiantDependentScopedBean bean) {

              System.out.println("transientVisit");

              bean.doSomething();

          }

       

      When I place that method in an application scoped bean I see that the bean is destroyed as soon as the method is finished - as I would expect from a method annotated @TransientReference. However when I move that method to my Servlet class (annotated @WebServlet, extending HttpServlet) I see transientVisit is called, but preDestroy is not called until I shut down the entire server.