1 Reply Latest reply on Jul 24, 2014 9:58 AM by Dawid Stępień

    CDI interceptor binding doesn't work

    Dawid Stępień Newbie

      Hi,

       

      I'm New to the forum so first of all I want to say hello. I'm learning JEE from few months and like every newbie I encounter issues. In most cases I find the solutions at google, books, forums, stackoverflow etc., but sometimes it doesn't work. So I'm here and I believe that I find few answers from the more experienced developers.

       

      Recently I created a little project with CDI examples. Everything works fine except features that are configured with deployment descriptor. My simple app works without interceptor bindings or alternatives defined in beans.xml. So my question is do I need to configure something else in the JBoss?

       

      Below I present my interceptor binding files. You can find all project here: https://github.com/dstepien/jee7-examples

       

      beans.xml

      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
             version="1.1" bean-discovery-mode="all">
          <interceptors>
              <class>pl.dawidstepien.jee.examples.ProfileInterceptor</class>
          </interceptors>
      </beans>
      
      

       

      ProfileInterceptor.java

      package pl.dawidstepien.jee.examples;
      
      import java.util.logging.Logger;
      
      import javax.annotation.PostConstruct;
      import javax.inject.Inject;
      import javax.interceptor.AroundInvoke;
      import javax.interceptor.Interceptor;
      import javax.interceptor.InvocationContext;
      
      @Interceptor @Profiling
      public class ProfileInterceptor {
      
        @Inject
        Logger logger;
      
        @PostConstruct
        public void logMethod(InvocationContext invocationContext) throws Exception {
          logger.info("\n\nWelcome, this is " + invocationContext.getTarget().getClass().getSimpleName() + "\n");
          invocationContext.proceed();
        }
      
        @AroundInvoke
        public Object profile(InvocationContext invocationContext) throws Exception {
          long initTime = System.currentTimeMillis();
          logger.info("ENTER: " + invocationContext.getMethod().getName());
      
          try {
            return invocationContext.proceed();
          } finally {
            long diffTime = System.currentTimeMillis() - initTime;
            logger.info(invocationContext.getMethod().getName() + " took " + diffTime + " millis");
          }
        }
      }
      
      

       

      DatePrinterBean.java

      package pl.dawidstepien.jee.examples;
      
      import java.util.logging.Logger;
      
      import javax.ejb.Remote;
      import javax.ejb.Stateless;
      import javax.inject.Inject;
      
      @Stateless
      @Remote(DatePrinter.class)
      @Profiling
      public class DatePrinterBean implements DatePrinter {
      
        @Inject
        Logger logger;
      
        @Inject @SimpleDate
        DateGenerator dateGenerator;
      
        @Override
        public void printDate() {
          logger.info("Today's date: " + dateGenerator.generateDate());
        }
      }
      
      

       

      Profiling.java

      package pl.dawidstepien.jee.examples;
      
      import static java.lang.annotation.ElementType.METHOD;
      import static java.lang.annotation.ElementType.TYPE;
      import static java.lang.annotation.RetentionPolicy.RUNTIME;
      
      import java.lang.annotation.Retention;
      import java.lang.annotation.Target;
      
      import javax.interceptor.InterceptorBinding;
      
      @InterceptorBinding
      @Retention(RUNTIME)
      @Target({TYPE, METHOD})
      public @interface Profiling { }
      
      

       

      Thanks,

      Dawid