注入@Provider的JavaEE CDI和JAXRS给出了UnsatisfiedDependencyException

时间:2017-06-26 00:08:15

标签: java java-ee jax-rs cdi

我有一个JAXRS提供商:

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthFilter implements ContainerRequestFilter {

    @Inject
    private AuthSecurityContext authSecurityContext;

    // ...

}

我的SecurityContext实现:

@RequestScoped
public class AuthSecurityContext implements SecurityContext {

    // ...

}

我正在使用glassfish作为我的容器,但它抛出了以下异常:

2017-06-26T01:04:49.139+0100|Warning: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 1
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=AuthSecurityContext,parent=AuthFilter,qualifiers={},position=-1,optional=false,self=false,unqualified=null,2049394753)

我的其他豆子被发现并被注射但是@Provider中的这个豆子不起作用,我错过了什么?

1 个答案:

答案 0 :(得分:2)

据我所知,问题是我在beans.xml中有bean-discovery-mode="annotated",JAX-RS注释类不会由容器的CDI实现自动管理,因此有两种可能的解决方案:

设置bean-discovery-mode="all"

OR

javax.enterprise.context.Dependent注释添加到AuthFilter,让CDI知道它是一个托管对象。