我试图用@EJB注释注入EJB:
>当我将EJB注入到同一个耳朵中的另一个EJB中时,它工作正常.
>当我将EJB从另一个耳朵注入到另一个EJB中时
服务器我得到一个例外:
EJB threw an unexpected (non-declared) exception during invocation of
method “sayHello”. Exception data: javax.ejb.EJBException: Injection
failure; nested exception is:
com.ibm.ejs.container.EJBNotFoundException: EJB with interface
com.mycompany.myapp.ejb.test2 not present in application myapp1
我正在使用WebSphere 7和EJB 3.0.当我自己做一个jndi查找,它工作正常.如何让容器知道从哪里注入远程bean?
myapp1.ear包含以下内容:myapp1.jar(EJB在哪里)
myapp1 EJB:
package com.mycompany.myapp1.ejb.test1; @Remote public interface HelloEjb1 { public String sayHello(); }
EJB Impl:
package com.mycompany.myapp.ejb.test1; @Stateless public class HelloEjbImpl1 implements HelloEjb1 { @EJB HelloEjb2 helloEjb2; @Override public String sayHello() { return HelloEjb2.sayHello(); } }
myapp2.ear包含以下内容:myapp2.jar(EJB在哪里)
myapp2 EJB:
package com.mycompany.myapp2.ejb.test2; @Remote public interface HelloEjb2 { public String sayHello(); }
EJB Impl:
package com.mycompany.myapp2.ejb.test2; @Stateless public class HelloEjbImpl2 implements HelloEjb2 { @Override public String sayHello() { return "Hello"; } }
解决方法
在部署应用程序(
1,2)时指定绑定,或在客户端(myapp1)EJB模块中包含
META-INF/ibm-ejb-jar-bnd.xml.它看起来像这样:
<ejb-jar-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd" version "1.0"> <session name="HelloEjbImpl1"> <ejb-ref name="com.mycompany.myapp.ejb.test1/helloEjb2" binding-name="myapp2/myapp2.jar/HelloEjbImpl2#com.mycompany.myapp1.ejb.test1.HelloEjb1" </session> </ejb-jar-bnd>
如果使用@EJB(name =“myrefname”),则可以简化ejb-ref name =“…”.如果您的myapp2.jar包含与< interface class =“...”binding-name =“...”/>的类似绑定文件,则可以简化绑定名称.元件.