Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-2131

Multiple instances of interceptors created for same target

      Using a Service class like this:

      @Interceptors(MyInterceptor.class)
      public class TestService {
      ...
      }
      
      @ManagedBean
      public class MyInterceptor {
      Object field;
      @AroundConstruct
          public void wrapConstructor(InvocationContext ctx) throws Exception {
               field = new Object();
      }
       @AroundInvoke
          public Object guard(final InvocationContext context) throws Exception {
              assert field != null;
      }
      }
      

      I would expect the assert to pass. However it fails, because weld creates two instances of this interceptor, one for each Interception Type.

      I did not see this documented, and I am not sure if it is a bug. If not, more documentation would help.

      JSR 318 seems to say this:
      http://download.oracle.com/otndocs/jcp/interceptors-1_2-mrel2-spec/
      "Except as noted below, when the target instance is created, a corresponding interceptor instance is created for each associated interceptor class."

      "The applicability of a method-level interceptor to more than one method of an associated target class does not affect the relationship between the interceptor instance and the target class—only a single instance of the interceptor class is created per target class instance"

            [WELD-2131] Multiple instances of interceptors created for same target

            For the record - it does not work for interceptors that are CDI beans either. Although the interceptor spec is not entirely clear, the team agreed that it should be fixed.

            Martin Kouba added a comment - For the record - it does not work for interceptors that are CDI beans either. Although the interceptor spec is not entirely clear, the team agreed that it should be fixed.

            This was java version "1.8.0_77" on Ubuntu 15.10.
            I could reproduce with tests running org.jglue.cdi-unit:cdi-unit:3.1.3, which in turn uses org.jboss.weld.se:weld-se-core:jar:2.2.13

            Thibault Kruse (Inactive) added a comment - This was java version "1.8.0_77" on Ubuntu 15.10. I could reproduce with tests running org.jglue.cdi-unit:cdi-unit:3.1.3, which in turn uses org.jboss.weld.se:weld-se-core:jar:2.2.13

            Hi tkruse_jira, thanks for noticing. It looks like a bug. What environment do you use? Also I would expect this will work for interceptors that are CDI beans (i.e. using @Interceptor and interceptor bindings).

            Martin Kouba added a comment - Hi tkruse_jira , thanks for noticing. It looks like a bug. What environment do you use? Also I would expect this will work for interceptors that are CDI beans (i.e. using @Interceptor and interceptor bindings).

              mkouba@redhat.com Martin Kouba
              tkruse_jira Thibault Kruse (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: