首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenEJB 4.5.1: NameNotFoundException

OpenEJB 4.5.1: NameNotFoundException
EN

Stack Overflow用户
提问于 2012-12-22 12:50:40
回答 1查看 1.3K关注 0票数 1

这是我第一次使用OpenEJB容器系统。当我使用InitialContext的锁定方法时,我得到了一个NameNotFoundException。我阅读了许多示例和教程,在每个示例中,查找方法如下所示:

代码语言:javascript
复制
initialContext.lookup("NameOfBean");

现在,我找到了另一个解决方案,它使用查找,如下面的代码片段,它也适用于我。

代码语言:javascript
复制
initialContext.lookup("java:global/classpath.ear/ProjectName/NameofBean");

问题是为什么第一个版本对我不起作用,我做错了什么?

OpenEJB日志的摘录:

代码语言:javascript
复制
INFO - ********************************************************************************
INFO - OpenEJB http://openejb.apache.org/
INFO - Startup: Sat Dec 22 13:17:59 CET 2012
INFO - Copyright 1999-2012 (C) Apache OpenEJB Project, All Rights Reserved.
INFO - Version: 4.5.1
INFO - Build date: 20121209
INFO - Build time: 08:47
INFO - ********************************************************************************
INFO - openejb.home = D:\workspace\ProjectName
INFO - openejb.base = D:\workspace\ProjectName
INFO - Succeeded in installing singleton service
INFO - Cannot find the configuration file [conf/openejb.xml].  Will attempt to create  one for the beans deployed.
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Using 'openejb.deployments.classpath.include=.*'
INFO - Found EjbModule in classpath: D:\workspace\ProjectName\build\classes
INFO - Searched 17 classpath urls in 2184 milliseconds.  Average 128 milliseconds per url.
INFO - Beginning load: D:\workspace\ProjectName\build\classes
INFO - Configuring enterprise application: D:\workspace\ProjectName\classpath.ear
WARNUNG - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime.
INFO - Auto-deploying ejb NameOfBean: EjbDeployment(deployment-id=NameOfBean)
[... AUTHORS'S NOTE: SOME MORE BEANS]
INFO - Assembling app: D:\workspace\ProjectName\classpath.ear
INFO - Hibernate Validator 4.2.0.Final
INFO - Ignoring XML configuration.
JAVA AGENT NOT INSTALLED. The JPA Persistence Provider requested installation of a ClassFileTransformer which requires a JavaAgent.  See http://openejb.apache.org/3.0/javaagent.html
INFO - OpenJPA dynamically loaded a validation provider.
INFO - Jndi(name=NameOfBeanRemote) --> Ejb(deployment-id=NameofBean)
INFO - Jndi(name=global/classpath.ear/ProjectName/NameOfBean!de.mypath.stateless.NameOfBeanInterface) --> Ejb(deployment-id=NameofBean)
INFO - Jndi(name=global/classpath.ear/ProjectName/NameofBean) --> Ejb(deployment-id=NameOfBean)
[... AUTHORS'S NOTE: SOME FOR OTHER BEANS]
INFO - OpenWebBeans Container is starting...
INFO - Adding OpenWebBeansPlugin : [CdiPlugin]
INFO - All injection points are validated successfully.
INFO - OpenWebBeans Container has started, it took 250 ms.
INFO - Created Ejb(deployment-id=NameOfBean, ejb-name=NameOfBean, container=Default Stateless Container)
[... AUTHORS'S NOTE: SOME FOR OTHER BEANS]
INFO - Quartz scheduler 'OpenEJB-TimerService-Scheduler' initialized from an externally provided properties instance.
INFO - Quartz scheduler version: 2.1.6
INFO - Scheduler OpenEJB-TimerService-Scheduler_$_OpenEJB started.
INFO - Started Ejb(deployment-id=NameOfBean, ejb-name=NameOfBean, container=Default Stateless Container)
[... AUTHORS'S NOTE: SOME FOR OTHER BEANS]

这是我的TestClass:

代码语言:javascript
复制
public class NamerOfBeanOpenEJBTest {

private static InitialContext initialContext;

@BeforeClass
public static void setUp() throws Exception {
       Properties properties = new Properties();
       properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
       properties.setProperty("openejb.deployments.classpath.include", ".*");
               initialContext = new InitialContext(properties);
}
@Test
public void testBean() throws NamingException{  
    Object object = initialContext.lookup("java:global/classpath.ear/ProjectName/NameOfBean");
    assertNotNull(object);
    assertTrue(object instanceof NameOfBean);
}

@AfterClass
public static void afterClass() throws Exception {
    if (initialContext != null) {
        initialContext.close();
    }}
}

有人给我小费或解决方案吗?非常感谢。

编辑:

在作为7.1的JBoss中,查找可以放置如下示例:

代码语言:javascript
复制
new InitialContext().lookup("ejb:/ProjectName//NameOfBean!de." + "mypath.sessionbean.stateless.NameOfBeanInterface");

这在OpenEJB中不可能吗?是否必须更改每个bean中的每个查找调用才能使用OpenEJB进行本地测试?这样做既不有效,又节省时间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-28 15:37:57

问题解决了!

查找的结构是{deploymentId}{interfaceType.annotationName}。因此,在我的情况下,它必须是

代码语言:javascript
复制
initialContext.lookup("NameOfBeanLocal");

或initialContext.lookup("NameOfBeanRemote");取决于接口的类型。

要解决JBoss的问题,可以从默认查找切换到

代码语言:javascript
复制
new InitialContext().lookup("ejb:/ProjectName//NameOfBean!de." + "mypath.sessionbean.stateless.NameOfBeanInterface");

对于一些更灵活的东西,如Dependcy-查找或依赖项注入,并使用@EJB注释。这两种方式都得到JBoss和OpenEJB的支持。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14002982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档