BeanTest icon indicating copy to clipboard operation
BeanTest copied to clipboard

Injection in EJB interceptors is not working

Open agori opened this issue 11 years ago • 4 comments

PersistenceContext is not injected in EJB interceptors, and throws NullPointerException when I try to access the EntityManager. This issue forces me to rewrite all the interceptors.

public class MyInterceptor {

    @PersistenceContext EntityManager em;

    @AroundInvoke
    public Object handle(InvocationContext context) throws Exception {
        try {
            return context.proceed();
        } finally {
            em.flush();  // throws NPE!
        }
    }
}

agori avatar May 29 '14 07:05 agori

Thanks for creating the issue.

We will take care of it in the next release (0.2).

carlosbarragan avatar Jun 02 '14 11:06 carlosbarragan

At the moment I´am fixing the NPE :bug: in https://github.com/qabbasi/BeanTest/tree/Issue%233 :wink:

qaiser42 avatar Oct 16 '14 13:10 qaiser42

Tried the following:

public void processAfterBeanDiscovery(@Observes AfterTypeDiscovery afterTypeDiscovery, BeanManager beanManager) { List<Class<?>> interceptors = afterTypeDiscovery.getInterceptors(); }

But AfterTypeDiscovery#getInterceptors returns an empty Interceptor list.

Ref: http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/AfterTypeDiscovery.html

qaiser42 avatar Oct 16 '14 14:10 qaiser42

Alternative approach: retrieve via the @Interceptors(Class[]) annotation all old fashioned Interceptor definitions altogether with injection points such as the @PersistenceContext.

Old fashioned interceptorbinding

@Interceptors(MyInterceptor.class) public class MyInterceptedBean {}

Sample interceptor (without @InterceptorBinding)

public class MyInterceptor {

@PersistenceContext <-- replaced by @Inject
EntityManager em;

@AroundInvoke
public Object handle(InvocationContext context) throws Exception {
    try {
        return context.proceed();
    } finally {
        em.flush();
    }
}

}

qaiser42 avatar Oct 16 '14 14:10 qaiser42