在JBoss7上的EAR之间的EJB注入

时间:2013-08-23 10:54:09

标签: java java-ee jboss cdi

是否可以在同一应用程序服务器中从另一只耳朵注入StatelessBean而无需进行JNDI查找?作为应用服务器,我们使用的是JBoss 7.2。

我有以下设置:

EAR1
│   services1-0.1.jar
│   web-0.1.war
│
├───lib
│       EAR2-SERVICES-api-0.1.jar
│
└───META-INF
        application.xml
        beans.xml


EAR2
│   EAR2-SERVICES-impl-0.1.jar
│
├───lib
│       EAR2-SERVICES-api-0.1.jar
│
└───META-INF
        application.xml
        beans.xml

EAR2包含例如以下服务:

@Named
@Stateless
public class ServiceBean implements Service { }

和界面:

@Remote
interface Service { }

来自EAR1的调用者应该只考虑API而不是实现。我如何实现,这是保证。当我必须使用JNDI名称时,我必须知道实现的位置。

为了将 Service 注入EAR1,我尝试了 @Inject @EJB 。但我总是得到Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Service] with qualifiers [@Default] at injection point [[field] @Inject package.ServiceBean.service]

MANIFEST.MF依赖于deployment.EAR2.ear

1 个答案:

答案 0 :(得分:2)

当应用程序服务器的类加载器隔离这两个应用程序时,EAR1无法看到服务接口的实现,这就是为什么它抱怨不满意的依赖项。我建议你在EAR1中实现一个查找的生成器方法,以及在EAR2中调用远程ejb的ejb代理。这个page描述了如何查找远程ejb。生产者方法看起来像这样:

 @Produces
 public Service produceService() {
   Properties jndiProps = new Properties();
   jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
   jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
   // create a context passing these properties
   Context ctx = new InitialContext(jndiProps);
   // lookup
   Service service = (Service) ctx.lookup("<jndi name of the ejb>");
   return service;
}

这将满足CDI抱怨的依赖。