2 Replies Latest reply on Jul 9, 2014 2:56 PM by alex soto

    CDI Interceptor not invoked

    alex soto Newbie



      I am testing a simple CDI interceptor with WildFly 8.0.0.Final  and it is not being invoked.

      I only have the following dependencies:




      My Annotation is:


      package org.interceptor.test;
      import static java.lang.annotation.ElementType.METHOD;
      import static java.lang.annotation.ElementType.TYPE;
      import java.lang.annotation.Inherited;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      import javax.interceptor.InterceptorBinding;
      @Target({METHOD, TYPE})
      public @interface MethodLogger {
          String action();


      The interceptor is this:


      package org.interceptor.test;
      import javax.interceptor.AroundInvoke;
      import javax.interceptor.Interceptor;
      import javax.interceptor.InvocationContext;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      @MethodLogger(action = "")
      public class MethodInterceptor {
        private static Logger log = LoggerFactory.getLogger(MethodInterceptor.class);
          public Object aroundInvoke(InvocationContext invocation) throws Exception {
          log.info("**** BEFORE **** ");
        Object result = invocation.proceed();
          log.info("**** AFTER **** ");
          return result;


      The CDI bean subject of interception is:


      package org.interceptor.test;
      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      import javax.inject.Named;
      public class DatabaseManager {
          @MethodLogger(action = "Truncate Table")
          public String truncateTable() {
        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Table has been truncated."));
              return "";

      My beans.xml file in the WEB-INF directory is this:


      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             version="1.1" bean-discovery-mode="all">

      My JSF Page where the CDI bean method is invoked is here:


      <!DOCTYPE html>
      <html xmlns="http://www.w3.org/1999/xhtml"
          <title>Sample Application</title>
          <h:form id="myForm">
          <h1>Demo Application</h1>
            <h:commandButton action="#{databaseManager.truncateTable}" value="Click to test method" />


      The databaseManager method is invoked but the interceptor is not.

      What am I doing wrong?




        • 1. Re: CDI Interceptor not invoked
          alex soto Newbie

          Interesting that if I remove the annotation "action" parameter it starts working.

          In my case, I need to have some parameters in the annotation, so the interceptor can use this information.


          Are parameterized annotations supported for CDI interceptors?

          • 2. Re: CDI Interceptor not invoked
            alex soto Newbie

            Problem solved thanks to a colleague recommendation. 

            Needed to add the @Nonbining qualifier to the annotation property.


            @Target({METHOD, TYPE})
            public @interface MethodLogger {
                @Nonbinding String action();


            Without the @Nonbinding keyword, the interceptor is not activated.