我已經做了一個簡單的應用程序來測試這個問題,我在小規模。我有一個EJB:注入ejb到託管bean導致BeanInstantiationException
@Local
public interface PersonaDAO {
public void sayHello(Persona persona);
}
@Stateless
public class PersonaDAOImpl implements PersonaDAO {
private PersonaDAOImpl() {
}
public void sayHello(String nombre) {
System.out.println("HELLO " + nombre + " welcome to EJB 3!");
}
}
,我得到了一個bean管理JSF:
@ManagedBean(name="loginBean")
@ViewScoped
public class LoginBean extends PageBean {
private String nombre;
@EJB
private PersonaDAO dao;
public String confirmar()
{
String outcome = null;
Persona persona = new Persona();
persona.setNombre(nombre);
dao.sayHello(persona);
return outcome;
}
.....
}
我得到這個錯誤部署:
DEPLOYMENTS IN ERROR:
Deployment "vfs:///home/shapacko/ambiente/jboss-6.0.0.Final/server/default/deploy/Prueba.war" is in error due to the following reason(s): java.lang.RuntimeException: Could not resolve @EJB reference: [EJB Reference: beanInterface 'com.application.business.ServicioPersonasImpl', beanName 'null', mappedName 'null', lookupName 'null', owning unit '[email protected]{vfs:///home/shapacko/ambiente/jboss-6.0.0.Final/server/default/deploy/Prueba.war}'] for environment entry: env/com.application.presentation.seguridad.LoginBean/sp in unit [email protected]{vfs:///home/shapacko/ambiente/jboss-6.0.0.Final/server/default/deploy/Prueba.war}
然後,如果我跑我得到的應用程序:
javax.servlet.ServletException: javax.ejb.EJBException: java.lang.RuntimeException: org.jboss.ejb3.instantiator.spi.BeanInstantiationException: Could not create new instance with no arguments of: class com.application.persistence.PersonaDAOImpl
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
我不明白wh在這個問題上。這種注射是可能的嗎?或者我需要做一個jndi查找而不是注入ejb?