2016-09-22 79 views
1

我正在使用Payara 4.1.1.161。我有一个Jersey @Path JAX-RS资源,我想要做的就是@使用CDI向它注入一个bean。我尝试了很多不同的组合来实现这个目标,但到目前为止,我成功的唯一方法是在beans.xml中设置bean-discovery-mode =“all”。将@Inject Bean注入Jersey @Path JAX-RS资源需要bean-discovery-mode =“all”吗?

我知道“annotated”是首选的模式,没有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; 
    } 
} 

再次,如果我有我的应用程序如上面,并用豆发现模式=配置“所有”一切似乎是确定和应用部署没有错误当调用JAX-RS服务时,bean被注入时没有错误。但是当我切换到bean-discovery-mode =“annotated”或者如果我没有beans.xml文件,那么事情就会变得非常糟糕。

那么你可以@注入一个bean到运行Payara 4.1.1.161的Jersy @Path JAX-RS资源中,使用no beans.xml还是使用bean-discovery-mode =“annotated”?

回答

4

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

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

例如;

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

请注意:JAX-RS注释不属于将类转换为CDI bean的注释。并非所有注释都做,只有CDI范围注释和其他一些注释。以下是列表:http://stackoverflow.com/a/29167950/784594。必需的bean('InjectMe')和需要该bean('InjectResource')的bean都必须使用其中的一个注释。 – OndrejM

+0

谢谢。我必须尝试每种注释组合,但我不认为我注释了JAX-RS端点本身。我99.9%的正面评论认为@Path注释将其转化为CDI托管资源,但我当然错了。再次感谢。 –