【问题标题】:Is bean-discovery-mode="all" required to @Inject a bean into a Jersey @Path JAX-RS resource?@Inject bean 到 Jersey @Path JAX-RS 资源中是否需要 bean-discovery-mode="all"?
【发布时间】:2017-01-31 14:33:57
【问题描述】:

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

我知道“带注释”是首选模式,没有 beans.xml 更受欢迎。但每次我尝试使用“注释”时,我要么调用 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 文件时,事情就会变得非常糟糕。

那么,您能否在没有 beans.xml 或 bean-discovery-mode="annotated" 的情况下将 bean @Inject 到运行 Payara 4.1.1.161 的 Jersy @Path JAX-RS 资源中?

【问题讨论】:

    标签: java dependency-injection jax-rs cdi payara


    【解决方案1】:

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

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

    例如;

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

    【讨论】:

    • 请注意:JAX-RS 注释不属于将类转换为 CDI bean 的注释。并非所有注释都可以,只有 CDI 范围注释和其他一些注释。这是列表:stackoverflow.com/a/29167950/784594。所需的 bean (InjectMe) 和需要 bean (InjectResource) 的 bean 都必须使用其中之一进行注释。
    • 谢谢。我一定尝试过每种注释组合,但我认为我没有对 JAX-RS 端点本身进行注释。我 99.9% 肯定 @Path 注释将它变成了 CDI 托管资源,但我当然错了。再次感谢。
    猜你喜欢
    • 2015-02-07
    • 2013-12-07
    • 2016-08-20
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多