我试图通过ServiceLocator进行EJB查找,而不硬编码JNDI名称,只使用本地接口名称。问题是EJB分布在其他模块(JAR)中。例如,我有这样一个场景:
项目xxx
@Stateless
class EjbXBean implements EjbX {
}
java:global/project-xxx/EjbXBean
项目yyy
:
@Stateless
class EjbYBean implements EjbY {
}
java:global/project-yyy/EjbYBean
EjbX和EjbY都是
@Local
EjbX ejbx = ServiceLocator.lookup(EjbX.class);
EjbY ejby = ServiceLocator.lookup(EjbY.class);
EjbX ejbx = ServiceLocator.lookup("java:global/project-yyy/EjbXBean");
EjbY ejby = ServiceLocator.lookup("java:global/project-yyy/EjbYBean");
我试图找出这种情况下的最佳实践是什么,因为我不知道硬编码的JNDI名称在JavaEE世界中是否是一种好的实践。
我使用OpenEjb 4.7.4进行开发和集成测试,使用Wildfly 10.1.0进行生产。
我可以在Wildfly 10.1.0中使用CDI来实现这一点:
@Override
public Object lookup(Class<?> type, Annotation... annotations) throws NamingException {
BeanManager manager = CDI.current().getBeanManager();
Iterator<Bean<?>> beans = manager.getBeans(type, annotations).iterator();
if (!beans.hasNext()) {
throw new NamingException("CDI BeanManager cannot find an instance of requested type " + type.getName());
}
Bean<?> bean = beans.next();
CreationalContext<?> ctx = manager.createCreationalContext(bean);
return manager.getReference(bean, type, ctx);
}
MyClass.lookup(EjbX.class);
但我不想使用CDI,因为
I had some problems to put this to work