Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection in EJB interceptors is not working #3

Open
agori opened this issue May 29, 2014 · 4 comments
Open

Injection in EJB interceptors is not working #3

agori opened this issue May 29, 2014 · 4 comments
Labels

Comments

@agori
Copy link
Contributor

agori commented May 29, 2014

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!
        }
    }
}
@carlosbarragan
Copy link
Member

Thanks for creating the issue.

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

@bbq2100 bbq2100 self-assigned this Oct 16, 2014
@bbq2100
Copy link
Contributor

bbq2100 commented Oct 16, 2014

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

@bbq2100
Copy link
Contributor

bbq2100 commented Oct 16, 2014

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

@bbq2100
Copy link
Contributor

bbq2100 commented Oct 16, 2014

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();
    }
}

}

@bbq2100 bbq2100 added the bug label Oct 29, 2014
@bbq2100 bbq2100 changed the title Injection on EJB interceptors is not working Injection in EJB interceptors is not working Jan 15, 2015
@bbq2100 bbq2100 removed their assignment Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants