它是我第一次使用OpenEJB容器系统。当我使用InitialContext的锁定方法时,我会得到NamenotfoundException。我已经阅读了很多示例和教程,在每个示例中,查找方法看起来像:
initialContext.lookup("NameOfBean");
现在,我找到了另一个使用查找的解决方案,如以下代码段,对我也有用。
initialContext.lookup("java:global/classpath.ear/ProjectName/NameofBean");
问题是为什么第一个版本对我不起作用以及我做错了什么?
openejb log的摘录:
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:workspaceProjectName
INFO - openejb.base = D:workspaceProjectName
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:workspaceProjectNamebuildclasses
INFO - Searched 17 classpath urls in 2184 milliseconds. Average 128 milliseconds per url.
INFO - Beginning load: D:workspaceProjectNamebuildclasses
INFO - Configuring enterprise application: D:workspaceProjectNameclasspath.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:workspaceProjectNameclasspath.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]
这是我的测试频道:
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();
}}
}
有人为我有Tipps或解决方案吗?非常感谢。
编辑:
在JBOSS AS 7.1中,查找可以像此示例一样放置:
new InitialContext().lookup("ejb:/ProjectName//NameOfBean!de." + "mypath.sessionbean.stateless.NameOfBeanInterface");
在OpenEJB中难道吗?我是否必须更改每个Bean中的每个查找电话才能使用OpenEJB进行本地测试?那不是真的有效且节省时间。
解决问题!
查找的strukture是{deploymentId} {InterfaceType.annotationName}。因此,就我而言,它必须是
initialContext.lookup("NameOfBeanLocal");
或 initialContext.lookup(" nameofbeanRemote");取决于接口的类型。
为了解决jboss解决的问题,您可以从默认查找
切换new InitialContext().lookup("ejb:/ProjectName//NameOfBean!de." + "mypath.sessionbean.stateless.NameOfBeanInterface");
对更灵活的东西,例如依赖环境或依赖项注入,并使用@EJB注释。两种方式都是JBOSS和OPENEJB的支持。