我刚刚在Eclipse中使用Web模块和EJB3模块创建了一个简单的企业应用程序项目。
这是我的 EJB :
@Stateless
public class MyBean implements MyBeanRemote {
public String getGreeting(){return "Hello World!";}
}
这是我的远程业务接口:
@Remote
public interface MyBeanRemote {
String getGreeting();
}
这是我的 Servlet :
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyBeanRemote myBean = null;
try {
myBean=(MyBeanRemote) new InitialContext().lookup("XXX"); //1
} catch (NamingException e) {e.printStackTrace();}
response.getWriter().append(myBean.getGreeting());
}
}
Bean注入就像一个魅力,但我无法成功执行JDNI查找:应该在行//1
而不是XXX
插入什么?我尝试过任何可能的组合(好吧,显然错过了正确的组合......)
据我所知,Geronimo使用OpenEJB将名称映射到资源,而at this page则可以读取
默认的JNDI名称采用以下格式:
{deploymentId}{interfaceType.annotationName}
所以我认为这只是MyBeanRemote
,但那不起作用......我错过了什么?我花了最后几个小时尝试不成功,并在SO上阅读类似的答案。
这是我的 openejb.jar.xml :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
<dep:environment>
<dep:moduleId>
<dep:groupId>default</dep:groupId>
<dep:artifactId>MyProjectEJB</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
</dep:environment>
</ejb:openejb-jar>
由向导创建。
答案 0 :(得分:0)
作为用户 achabahe suggested,在这些question/answers的帮助下,我打印了整个JNDI树:
String getJNDITree() {
Context context=null;
try {
context = new InitialContext();
} catch (NamingException e1) {e1.printStackTrace();}
List<String>roots=Arrays.asList("java:comp","java:module","java:app","java:global");
StringBuilder tree=new StringBuilder();
for(String root:roots)
recursiveDeepVisit(context,root,"",tree);
return tree.toString()+"\n";
}
void recursiveDeepVisit(Context context,String path,String prefix,StringBuilder tree){
tree.append("\n"+prefix+path);
try {
NamingEnumeration<NameClassPair> list = context.list(path);
while (list.hasMore()) {
String innerPath = path+"/"+list.next().getName();
recursiveDeepVisit(context, innerPath, " "+prefix,tree);
}
} catch (Exception e) {}
}
获得此输出
java:
java:app
java:app/env
java:app/MyProjectEJB
java:app/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote
java:app/MyProjectEJB/MyBean
java:app/AppName
java:comp
java:comp/UserTransaction
java:comp/HandleDelegate
java:comp/ValidatorFactory
java:comp/TransactionManager
java:comp/Bundle
java:comp/TransactionSynchronizationRegistry
java:comp/env
java:comp/ORB
java:comp/Validator
java:comp/BundleContext
java:TransactionManager
java:TransactionSynchronizationRegistry
java:module
java:module/ModuleName
java:module/env
java:global
java:global/MyProject
java:global/MyProject/MyProjectEJB
java:global/MyProject/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote
java:global/MyProject/MyProjectEJB/MyBean
java:global/geronimo-mejb-3.0.1
java:global/geronimo-mejb-3.0.1/MEJB
java:global/geronimo-mejb-3.0.1/ejb
java:global/geronimo-mejb-3.0.1/ejb/mgmt
java:global/geronimo-mejb-3.0.1/ejb/mgmt/MEJB
java:global/geronimo-mejb-3.0.1/ejb/mgmt/MEJB!javax.management.j2ee.ManagementHome
java:global/geronimo-mejb-3.0.1/MEJB!javax.management.j2ee.ManagementHome
java:global/env
事实证明
MyBeanRemote myBean=(MyBeanRemote) new InitialContext().lookup("java:app/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote");
是我正在寻找的解决方案。