2 Replies Latest reply on Apr 11, 2013 3:30 AM by Håkan Fransson

    @Resource injection not working with Interceptor binding

    Håkan Fransson Newbie

      I'm new to JEE, EJB3 and CDI but have worked with EJB2.1 and we are going to convert an old system from EJB2.1 and Spring to EJB3 and Spring.

       

      I'm trying to create an Interceptor that rollback EJB3.1 CMT when throwing a checked exception.  In a clean Interceptor, it works fine.

       

      public class RollbackFacadeInterceptor {
      
          @Resource
          EJBContext ejbCtx;
      
          @AroundInvoke
          public Object businessIntercept(InvocationContext ctx) throws Exception {
               Object result = null;
              try {
                  result = ctx.proceed();
                  return result;
              } catch (Exception e) {
                  ejbCtx.setRollbackOnly();
                  throw e;
              } finally {
                  // PostInvoke: do something (cleanup, etc) after the main processing
                  // is done
              }
          }
      }
      

       

      Used in a EJB. This EJB inject Spring beans. Parent class have some Spring support stuff.

       

      @Stateless
      public class NoSpringTransFacade extends EJB3BaseFacade implements
              NoSpringTransFacadeLocal {
      
          @Autowired
          private NoSpringTransControllerImpl noSpringTransControllerImpl;
      
         @Interceptors(RollbackFacadeInterceptor.class)
         @Override
          public void throwCheckedExceptionDefaultRollbackFor() throws Exception {
              noSpringTransControllerImpl.throwCheckedExceptionDefaultRollbackFor();
          }
      }
      

       

       

      But if i'm going to use a CDI interceptor binding instead, then i get a NameNotFoundException when accessing the EJB from a web app.

       

       

      @Inherited
      @InterceptorBinding
      @Retention(RUNTIME)
      @Target({ METHOD, TYPE })
      public @interface RollbackFacadeInterceptorBinding {
      }
      

       

       

      @RollbackFacadeInterceptorBinding
      @Interceptor
      public class RollbackFacadeInterceptor {
      
          @Resource
          EJBContext ejbCtx;
      
          @AroundInvoke
          public Object businessIntercept(InvocationContext ctx) throws Exception {
      
              Object result = null;
              try {
                  result = ctx.proceed();
                  return result;
              } catch (Exception e) {
                  ejbCtx.setRollbackOnly();
                  throw e;        } finally {
                  // PostInvoke: do something (cleanup, etc) after the main processing
                  // is done
              }
          }
      }
      

       

       

      @Stateless
      public class NoSpringTransFacade extends EJB3BaseFacade implements
              NoSpringTransFacadeLocal {
      
          @Autowired
          private NoSpringTransControllerImpl noSpringTransControllerImpl;
      
         @RollbackFacadeInterceptorBinding
         @Override
          public void throwCheckedExceptionDefaultRollbackFor() throws Exception {
              noSpringTransControllerImpl.throwCheckedExceptionDefaultRollbackFor();
          }
      }
      

       

      beans.xml  in the projects META-INF folder

       

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
      
          <interceptors>
              <class>se.vhs.nya.architecturetest.interceptor.RollbackFacadeInterceptor</class>
          </interceptors>
      
      </beans>
      

       

       

      When accessing the EJB, a NameNotFoundException is thrown.

       

      10:11:52,925 INFO  [stdout] (http-executor-threads - 21) Caused by: javax.naming.NameNotFoundException: env/se.vhs.nya.architecturetest.interceptor.RollbackFacadeInterceptor/ejbCtx -- service jboss.naming.context.java.comp.NYA.ArchitectureTest-SNAPSHOT.NoSpringTransFacade.env."se.vhs.nya.architecturetest.interceptor.RollbackFacadeInterceptor".ejbCtx

       

      I wonder if I do somehting wrong here, any ideas?