我为动态Web项目创建了一个示例EJB模块。但是当我尝试访问使用EJB的Servlet时,我没有绑定EJB。
package com.za.tutorial;
import javax.ejb.Stateless;
@Stateless
public class ZASessionBean implements ZASessionBeanLocal {
public String demoSessionBean(){
return "demoSessionBean";
}
}
package com.za.tutorial;
import javax.ejb.Local;
@Local
public interface ZASessionBeanLocal {
public String demoSessionBean();
}
我已经创建了一个动态Web项目,我在Servlet中使用上面的EJB如下。
package com.za.tutorial;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ZaServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ZaServlet() {
super();
// TODO Auto-generated constructor stub
}
@EJB(mappedName="ZASessionBean/local")
ZASessionBeanLocal objZASessionBean;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("#ZaServlet#start");
PrintWriter out=response.getWriter();
out.println(objZASessionBean.demoSessionBean());
System.out.println("#ZaServlet#end");
}
}
我创建了一个带有EJB的Web,然后在JBoss 5中部署了Web项目。部署后我可以看到我的EJB部署在服务器控制台中。
ZEN_EAR/ZASessionBean/local - EJB3.x Default Local Business Interface ZEN_EAR/ZASessionBean/local-com.za.tutorial.ZASessionBeanLocal - EJB3.x Local Business Interface
但是当我按如下方式访问我的servlet时
我得到了EJB未绑定的异常,如下所示。
javax.naming.NameNotFoundException: ZASessionBean not bound
org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
org.jnp.server.NamingServer.getObject(NamingServer.java:785)
org.jnp.server.NamingServer.lookup(NamingServer.java:396)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:726)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
javax.naming.InitialContext.lookup(InitialContext.java:392)
org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1346)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:817)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
org.jboss.ejb3.JndiUtil.lookup(JndiUtil.java:44)
org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:75 )
org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:99 )
...........