1

是否可以在不进行 JNDI 查找的情况下从同一应用程序服务器中的另一只耳朵注入 StatelessBean?作为应用服务器,我们使用 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

4

1 回答 1

2

由于应用程序服务器的类加载器隔离了这两个应用程序,EAR1 无法看到服务接口的实现,这就是它抱怨依赖关系不满足的原因。我建议您在 EAR1 中实现一个生产者方法,用于查找和 ejb 代理,它将调用 EAR2 中的远程 ejb。本页介绍如何查找远程 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 抱怨的依赖关系。

于 2013-08-23T13:35:43.477 回答