Injecting ejb into managed bean causes BeanInstantiationException
I've made a simple application to test this problem i'm having in small scale. I've got an ejb:
@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!");
}
}
And i got a bean managing a 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;
}
.....
}
I'm getting this deployment error:
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 'AbstractVFSDeploymentContext@8203928{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 AbstractVFSDeploymentContext@8203928{vfs:///home/shapacko/ambiente/jboss-6.0.0.Final/server/default/deploy/Prueba.war}
And then if i run the app i get a:
javax.servlet.S开发者_如何学CervletException: 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)
I don't understand what's the problem. Is this injection possible? Or do i need to do a jndi lookup instead of injecting the ejb?
It is likely that the use of the private constructor for the Stateless Session Bean is the cause of the issue, as evidenced by the following exception entry:
org.jboss.ejb3.instantiator.spi.BeanInstantiationException: Could not create new instance with no arguments of: class com.application.persistence.PersonaDAOImpl
An exception of type BeanInstantiationException
is typically thrown when the container is unable to create an instance of the bean. In all likelihood, this is due to the declaration of a private constructor, and the unavailability of any other non-private no-arg constructor in the Stateless Session Bean. It is inferred that you will have to change the visibility of PersonaDAOImpl()
to public.
The EJB 3.1 specification states this quite explicitly:
4.9.2 Session Bean Class
The class must be defined as public, must not be final, and must not be abstract. The class must be a top level class.
The class must have a public constructor that takes no parameters. The container uses this constructor to create instances of the session bean class. The following are the requirements for the session bean class:
精彩评论