BeanManager(https://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html)的规范说“Java EE组件可以通过查找名称java:comp / BeanManager从JNDI获取BeanManager实例。”
我在Jboss 7中将以下代码写入类的静态初始化程序块,并按预期工作:
BeanManager beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
我尝试在WebLogic 12.1.2.0.0中执行相同操作,但出现以下错误:
javax.naming.NameNotFoundException: remaining name: comp/BeanManager
at weblogic.j2eeclient.SimpleContext.internalLookup(SimpleContext.java:77)
at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:39)
at weblogic.jndi.SimpleContext.lookup(SimpleContext.java:86)
at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:45)
at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:130)
at javax.naming.InitialContext.lookup(InitialContext.java:411)...
为什么它在WebLogic中不起作用?
更新
如果我拨打CDI.current()
方法,我会收到IllegalStateException
,这样也无济于事。
当我调用new InitialContext().lookup("java:comp/BeanManager")
方法时,InitialContext
不包含java:comp
命名空间,只包含java:global
命名空间。
答案 0 :(得分:0)
我遇到了同样的问题,看起来它是Weblogic中的一个错误。
" Bug 18026126 - STATIC VARIABLE BEANMANAGER不工作"
解
要实施此解决方案,请执行以下步骤:
以下是BUG说明:
STATIC VARIABLE BEANMANAGER不工作
private static BeanManagerBeanTest bmbs =
(BeanManagerBeanTest)BeanLookupUtil.getCdiBean("beanManagerBeanTest")
throws lookup exception and if we remove static it's working
public class BeanLookupUtil {
public static BeanManager getBeanManager(){
BeanManager beanManager = null;
try {
beanManager = (BeanManager)new
InitialContext().lookup("java:comp/BeanManager");
} catch (Exception e) {
e.printStackTrace();
}
return beanManager;
}
public static Object getCdiBean(String name){
Object obj = null;
try {
BeanManager bm = getBeanManager();
Set<Bean<?>> beanSet = bm.getBeans(name);
if(!beanSet.isEmpty()){
Bean<?> bean = beanSet.iterator().next();
CreationalContext<?> ctx = bm.createCreationalContext(bean);
obj = bm.getReference(bean, bean.getBeanClass(), ctx);
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
public class TestCtrl {
private static BeanManagerBeanTest bmbs =
(BeanManagerBeanTest)BeanLookupUtil.getCdiBean("beanManagerBeanTest");
public void beanmanagertest() {
BeanManager beanManager = null;
try {
//BeanManagerBeanTest bmbs =
(BeanManagerBeanTest)BeanLookupUtil.getCdiBean("beanManagerBeanTest");
bmbs.sum(10, 10);
} catch (Exception e) {
log.error("Lookup Error:", e);
}
}
答案 1 :(得分:0)
问题出现在WebLogic的开发人员版本上。在我们安装了它的中间件版本之后,它在12.1.2和1.2.1.3版本中都有效。