Module jakarta.cdi

Interface InterceptionFactory<T>

  • Type Parameters:
    T - type for which the wrapper is created

    public interface InterceptionFactory<T>
    InterceptionFactory allows to create a wrapper instance whose method invocations are intercepted by method interceptors and forwarded to a provided instance.

    An implementation of InterceptionFactory may be obtained by calling BeanManager.createInterceptionFactory(CreationalContext, Class) to be used in the create method of a custom bean for example.

     public class MyCustomBean implements Bean<MyClass> {
    
         BeanManager bm;
    
         public MyBean(BeanManager bm) {
            this.bm = bm;
         }
    
         public MyClass create(CreationalContext<MyClass> creationalContext) {
    
             InterceptionFactory<MyClass> factory = bm.createInterceptionFactory(creationalContext, MyClass.class);
    
             factory.configure().filterMethods(m -> m.getJavaMember().getName().equals("shouldBeTransactional")).findFirst()
                     .ifPresent(m -> m.add(new AnnotationLiteral<Transactional>() {
                     }));
    
             return factory.createInterceptedInstance(new MyClass());
         }
     }
     

    The container must also provide a built-in bean with scope Dependent, bean type InterceptionFactory and qualifier Default that can be injected in a producer method parameter.

     @Produces
     @RequestScoped
     public MyClass produceMyClass(InterceptionFactory<MyClass> factory) {
         factory.configure().add(new AnnotationLiteral<Transactional>() {
         });
         return factory.createInterceptedInstance(new MyClass());
     }
     

    Instances of this class are neither reusable nor suitable for sharing between threads.

    CDI Lite implementations are not required to provide support for InterceptionFactory.

    Since:
    2.0
    Author:
    Antoine Sabot-Durand