5

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?

bafitor
  • 51
  • 3

2 Answers2

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
ZeusSelerim
  • 935
  • 8
  • 12
  • @Inject does not work. An exception is raised: **UnsatisfiedDependencyException: There was no object available for injection ...**. – bafitor Jul 31 '13 at 11:21
  • Thats because your class is not a managed bean. I'm not sure can can make this Provider a ManagedBean, so you are probably going to have to create your own producer that does the EJB lookup and then you can inject anywhere (there are lots of articles showing how to do this) or you can just do the lookup in this class. – ZeusSelerim Aug 02 '13 at 15:18
0

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.

TheArchitect
  • 2,161
  • 1
  • 12
  • 16
  • I tried both solutions but it does not work. I got an **UnsatisfiedDependencyException: There was no object available for injection.**. Any idea? – bafitor Jul 31 '13 at 11:20
  • 1
    I've not seen this one before with GF3 so probably a problem in GF4. https://java.net/jira/browse/GLASSFISH-20597 says it's fixed in v4.0.1 - is that the version you are using? If not you can try some of the workaround suggestions at http://stackoverflow.com/questions/16216759/dependency-injection-with-jersey-2-0 Did you try removing the @Stateless or do you need it? – TheArchitect Jul 31 '13 at 15:31