7 Replies Latest reply on Apr 5, 2012 2:13 PM by gimbal2

    Interceptors for non EJB

    928516
      Hi,

      does the jee standard covers interceptors for non EJB classes? I'm using Glassfish and it worked for a plain java classes e.g.:

      @Interceptors(TraceInterceptor.class)
      public class PlainClass {

           public String doSomething() {
                return "somthing";
           }

      }

      Hoever when I use the intercepter with a DAO class that's depending an an EntityManager e.g.:

      @Interceptors(TraceInterceptor.class)
      public class PersonDao implements IPersonDao {

           @PersistenceContext
           private EntityManager em;

           @Override
           public Person getById(Integer id) {
                return em.find(Person.class, id);
           }

      }

      I get a NullPointer exeption because the Entity Manger is not injected.


      so my question is: does the jee standard covers interceptors for non EJB classes?



      Regards

      Sebastian
        • 1. Re: Interceptors for non EJB
          gimbal2
          And if you take the interceptor off, the EntityManager injection in the DAO does work?

          I would assume not, since it is not a server managed resource.
          • 2. Re: Interceptors for non EJB
            Kayaman
            925513 wrote:
            does the jee standard covers interceptors for non EJB classes? I'm using Glassfish and it worked for a plain java classes e.g.:
            Yes. As you answered yourself, interceptors work for regular POJOs.
            Hoever when I use the intercepter with a DAO class that's depending an an EntityManager e.g.:
            I get a NullPointer exeption because the Entity Manger is not injected.
            Injection however doesn't work for regular POJOs, they need to be managed beans (@Named for example).
            so my question is: does the jee standard covers interceptors for non EJB classes?
            That can't be your question really.
            • 3. Re: Interceptors for non EJB
              928516
              Hi,

              thanks for your response.

              When I remove the @Interceptors annotation from the PersonDao everything works fine including the injection of the entity manager. When I add the @Interceptors annotation I get a null pointer at em.find(Person.class, id).

              I’m new to JEE6 because I have worked with Spring the last 6 years. Accidently I never used the @Named annotation. Surprisingly that is no problem (at least in Glassfish).

              Anyhow I added the @Named annotation to the DAO but that not changed anything, I still got a null pointer. What’s wrong?


              @Interceptors(TraceInterceptor.class)
              @Named
              public class PersonDao implements IPersonDao {

                   @PersistenceContext
                   private EntityManager em;

                   @Override
                   public Person getById(Integer id) {
                        return em.find(Person.class, id);
                   }
              }


              @Stateless
              @Interceptors(TraceInterceptor.class)
              public class TestSrv implements ITestSrv {

                   @Inject
                   private IPersonDao personDao;

                   @Override
                   public Person getPerson(Integer id) {
                        return personDao.getById(id);
                   }
              }



              Best regards

              Sebastian
              • 4. Re: Interceptors for non EJB
                928516
                Hi,

                I have done a little research this morning and find out following:

                1.     As far as I see it is not required to annotate the class to inject with @Named. I found several examples in the web that convinced me that it is not necessary. By using @Inject the container is looking for an implementation of the interface or just instantiates the class. The only condition is that the class to inject is in the same classpath as the target class.
                2.     When I inject the entity manager by using a setter-method it works even with the interceptor and without the @Named annotation. I do not understand yet why it is working that way but not the over way around. Would be great if you could give me a hint.


                Best regards

                Sebastian
                • 5. Re: Interceptors for non EJB
                  gimbal2
                  925513 wrote:
                  When I remove the @Interceptors annotation from the PersonDao everything works fine including the injection of the entity manager.
                  That I don't understand, unless PersonDao is secretly an EJB. You can't just inject EntityManager instances into any class, it has to be something that is part of the JEE spec (like an EJB, MDB, Servlet, JSF managed bean, ...). Unless Glassfish has some injection magic that is not part of the specs :s
                  • 6. Re: Interceptors for non EJB
                    Kayaman
                    gimbal2 wrote:
                    That I don't understand, unless PersonDao is secretly an EJB. You can't just inject EntityManager instances into any class, it has to be something that is part of the JEE spec (like an EJB, MDB, Servlet, JSF managed bean, ...). Unless Glassfish has some injection magic that is not part of the specs :s
                    Well, we recently found out that JBoss 7.1 is quite lenient in that part, so that's not out of the question.
                    • 7. Re: Interceptors for non EJB
                      gimbal2
                      Kayaman wrote:
                      gimbal2 wrote:
                      That I don't understand, unless PersonDao is secretly an EJB. You can't just inject EntityManager instances into any class, it has to be something that is part of the JEE spec (like an EJB, MDB, Servlet, JSF managed bean, ...). Unless Glassfish has some injection magic that is not part of the specs :s
                      Well, we recently found out that JBoss 7.1 is quite lenient in that part, so that's not out of the question.
                      Really? Hm, I have to revisit the spec I see... JEE6 was supposed to make servers more compatible, I can't imagine that there are differences in the ruling when injections work and when they do not. But are you talking about the old style injections or CDI injections here?