2016-09-22 65 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託管資源,但我當然錯了。再次感謝。 –