0

I cannot seem to get anything injected via @EJB or @Inject into Jersey. When I use @EJB/@Inject in the FilterFactory the field remains null, but injecting into other beans works fine.

I can successfully inject using @Context for the FIlterFacory.

What am I missing here?

public class FilterFactory implements ResourceFilterFactory{

    @EJB
    private MyFilter myFilter ;

    @Override 
    public List<ResourceFilter> create(AbstractMethod am) {
        List<ResourceFilter> filters = new ArrayList<ResourceFilter>();
        filters.add(myFilter);
        return filters;
    }

Part of my web.xml:

<init-param>
     <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
     <param-value>com.jea.openxchange.rest.filter.FilterFactory</param-value>
</init-param>

My filter

@Stateless
public class MyFilter implements ResourceFilter,ContainerResponseFilter  {

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        //todo
        return response;
    }

    @Override
    public ContainerRequestFilter getRequestFilter() {
        return null;
    }

    @Override
    public ContainerResponseFilter getResponseFilter() {
        return this;
    }  
}
Perception
  • 79,279
  • 19
  • 185
  • 195
busyBee
  • 533
  • 1
  • 5
  • 15

1 Answers1

1

This might be the issue of how Jersey creates an instance of FilterFactory. If it is not created by container, CDI will not work and @EJB will not also. As per Java EE6 CDI spec objects created by new or by reflection are not managed and thus no injection occures. New CDI spec will address this issue.

damiankolasa
  • 1,508
  • 9
  • 8
  • Thx for your reply! what would be the best to go forward? change to spring? or can i use InitialContext().lookup() for exmaple – busyBee Nov 27 '12 at 12:48
  • 1
    Context lookup will work :) but if your Bean has any Injects inside those will not be resolved. – damiankolasa Nov 27 '12 at 15:59