问题
I'm developing an REST application using Glassfish 4.0.
In resource classes I can get injection to work by making the class @Stateless and injecting via @EJB (injected class is an stateless EJB).
However this approach does not work in an JAX-RS filter. I cannot get injection to work at all.
See code below:
@Provider
public class UpdateFilter implements ContainerRequestFilter {
@EJB
private MyBeanInterface doStuffBean;
@Override
public void filter(ContainerRequestContext requestContext) {
...
}
}
doStuffBean is always null.
Any suggestions?
回答1:
I believe the @EJB only works in Java EE managed classes like other EJBs and Servlets.
If you are using CDI you could use @Inject annotation instead but if this class is not a ManagedBean then you will need to do a lookup.
回答2:
Try to use CDI by replacing @Stateless by @ManagedBean and @EJB by @Inject. This works for me in JAX-RS.
If you need EJB for other things than injection it may work for you to keep the double annotation @Stateless @ManagedBean.
来源:https://stackoverflow.com/questions/17951001/glassfish-4-jax-rs-filter-with-ejb