2011-07-10 98 views
2

我已經做了一個簡單的應用程序來測試這個問題,我在小規模。我有一個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?

回答

3

很可能使用了私有的構造爲無狀態會話bean是問題的原因,通過下面的例外項證明:

org.jboss.ejb3.instantiator.spi.BeanInstantiationException: Could not create new instance with no arguments of: class com.application.persistence.PersonaDAOImpl

BeanInstantiationException類型的異常通常是拋出當該容器無法創建該bean的實例。很可能,這是由於聲明瞭一個私有構造函數,以及在無狀態會話Bean中無法使用任何其他非私有無參數構造函數。據推測,你將不得不將PersonaDAOImpl()的可見性更改爲公開。

的EJB 3.1規範指出這很明確:

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: