是否需要 bean-discovery-mode="all" 将 bean @Inject 到 Jersey @Path JAX-RS 资源中?

Is bean-discovery-mode="all" required to @Inject a bean into a Jersey @Path JAX-RS resource?

我正在使用 Payara 4.1.1.161。我有一个 Jersey @Path JAX-RS 资源,我想做的就是使用 CDI 将一个 bean 注入其中。我已经尝试了很多不同的组合来让它工作,但到目前为止,我让它成功工作的唯一方法是在 beans.xml 中设置 bean-discovery-mode="all"。

我知道 "annotated" 是首选模式,没有 beans.xml 更受欢迎。但是每次我尝试使用 "annotated" 时,我要么调用 JAX-RS 资源失败,如下所示:

MultiException stack 1 of 1
org.glassfish.hk2.api.UnsatisfiedDependencyException: 
There was no object available for injection at
SystemInjecteeImpl(requiredType=InjectMe, parent=InjectResource,
qualifiers={}, position=-1, optional=false, self=false,
unqualified=null, 1000687916))

或者我部署应用程序失败,如下所示:

Exception during lifecycle processing
java.lang.Exception: java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.apache.catalina.LifecycleException:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: 
Unsatisfied dependencies for type InjectMe with qualifiers @Default
at injection point [BackedAnnotatedField] 
@Inject private org.thoth.jaspic.web.InjectResource.me

这是我的应用程序设置。

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

JAX-RS 应用程序

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationResourceConfig  extends org.glassfish.jersey.server.ResourceConfig {
    public ApplicationResourceConfig() {
        register(RolesAllowedDynamicFeature.class);
        registerClasses(
            org.thoth.jaspic.web.InjectResource.class
        );
     }
 }

JAX-RS 资源

@Path("inject")
public class InjectResource {
    @Inject
    private InjectMe me;

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getText(@Context SecurityContext context) {
        Principal p = context.getUserPrincipal();
        String retval = "<h3>inject</h3>";
        retval += String.format("<p>me=[%s]</p>", me);
        return retval;
    }
}

我要注入的简单bean

public class InjectMe implements Serializable {

    private static final long serialVersionUID = 158775545474L;

    private String foo;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }
 }

同样,如果我的应用程序如上面的配置和 bean-discovery-mode="all" 所示,一切似乎都正常,应用程序部署时没有错误,并且在调用 JAX-RS 服务时bean 被无误地注入。但是,当我切换到 bean-discovery-mode="annotated" 或者如果我根本没有 beans.xml 文件,那么事情就会变得非常糟糕。

那么你可以@Inject 一个 bean 到 Jersy @Path JAX-RS 资源 运行 Payara 4.1.1.161 没有 beans.xml 或者有 bean-discovery-mode="annotated"?

您的 JAX-RS 资源 class 需要一个 bean 定义注释才能启用 CDI bean 的注入。只需将 @ApplicationScoped@RequestScoped 添加到您的 JAX-RS 资源中,然后 bean 注入就可以在没有所有 bean 发现模式的情况下工作。

顺便说一句,我假设 InjectMe bean 也有某种形式的范围注释,因为它没有在上面的代码中显示。

例如;

@Path("inject")
@ApplicationScoped
public class InjectResource {