2014-02-27 31 views
0

雖然目前已經有相當長的一段StackOverflow的問題,博客文章等在網絡上,我仍然無法找出解決的辦法,以低於規定的問題。注入JAX-RS資源中的EJB到JBoss 5

這個問題(Injecting EJB within JAX-RS resource on JBoss7)我想注入一個EJB實例爲JAX-RS類相似。我與JBoss 5時,JBoss 7和8 WildFly嘗試我要麼得不到注入在所有(字段爲空),或服務器不部署(當我嘗試各種組合的註釋)。 添加@Stateless到JAX-RS使應用服務器知道,這兩個類爲豆類。但是,不發生注射。

是否有辦法注入的EJB爲REST應用程序?我可以提供什麼樣的信息(除了上面提到的問題中包含的信息)以幫助?

編輯:我創建了一個Github項目,顯示的代碼工作(與Glassfish 4.0)和不工作(與JBoss 5)。

https://github.com/C-Otto/beantest

  • 提交使用Glassfish的 4.0 4bf2f3d23f49d106a435f068ed9b30701bbedc9d工作。
  • 提交50d137674e55e1ceb512fe0029b9555ff7c2ec21使用澤西島1.8,這不起作用。
  • 提交86004b7fb6263d66bda7dd302f2d2a714ff3b939 使用澤西島2.6,這也不起作用。

EDIT2: 運行,我在JBoss 5試圖在Glassfish 4.0的代碼給出:

Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)] 
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)] 
    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:403) 

EDIT3:關鍵信息可能是我想的是在JBoss 5

有效的解決方案
+0

請張貼相關碼。 – Camilo

+0

我認爲這傢伙解釋得很好http://stackoverflow.com/questions/3027834/inject-a-ejb-into-jax-rs-restfull-service – ZeusSelerim

+0

實際上有一種方法,基本上你不必做任何特殊的事情來實現它。你的JAX-RS資源應該是一個EJB本身,這就足夠了。問題必須在代碼中的某處。所以我在這裏同意卡米洛。看到你的代碼可能有助於理解問題 – jjd

回答

2

如果您不想讓您的JAX-RS資源成爲EJB(@Stateless),然後使用@EJB@Resource來注入它,您可以隨時使用JNDI查找(我傾向於編寫一個「ServiceLocator」類通過其課程獲得服務

一個很好的資源,以瞭解有關的話題:

https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project

示例代碼:

try { 
    // 1. Retreive the Home Interface using a JNDI Lookup 
    // Retrieve the initial context for JNDI.  // No properties needed when local 
     Context context = new InitialContext(); 

     // Retrieve the home interface using a JNDI lookup using 
     // the java:comp/env bean environment variable  // specified in web.xml 
     helloHome = (HelloLocalHome) context.lookup("java:comp/env/ejb/HelloBean"); 

    //2. Narrow the returned object to be an HelloHome object.  // Since the client is local, cast it to the correct object type. 
    //3. Create the local Hello bean instance, return the reference 
     hello = (HelloLocal)helloHome.create(); 

    } catch(NamingException e) { 

    } catch(CreateException e) { 

    } 

每本身這不是「注入」,但你不使用「新「,然後讓應用程序服務器爲您提供一個受管理的實例。

我希望這是有用的,我不告訴你你已經知道的東西!

編輯:

這是一個很好的例子:https://docs.jboss.org/author/display/AS72/EJB+invocations+from+a+remote+client+using+JNDI

編輯2:
正如您在您的評論說,你想通過註釋注入它。 如果JNDI查找正在爲你工作沒有問題,並 如果您使用的是Java EE 6+(我猜你),你可以做到以下幾點:

@EJB(lookup = "jndi-lookup-string-here") 
private RemoteInterface bean; 
+0

不幸的是,這是我們目前的解決方法。我希望使用註釋並添加@Stateless或類似的東西沒有幫助。 –

+0

我已經爲你更新了我的答案@ C-Otto –