java - 将 ejb 注入(inject)托管 bean 会导致 BeanInstantiationException

标签 java ejb cdi

我制作了一个简单的应用程序来小规模地测试我遇到的这个问题。我有一个 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!");
}
}

我有一个管理 jsf 的 bean:

@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 '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}

然后,如果我运行该应用程序,我会得到:

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)

我不明白这是什么问题。这种注入(inject)可以吗?或者我是否需要进行 jndi 查找而不是注入(inject) ejb?

最佳答案

问题的原因可能是使用无状态 session Bean 的私有(private)构造函数,如以下异常条目所示:

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

当容器无法创建 bean 实例时,通常会引发 BeanInstantiationException 类型的异常。这很可能是由于私有(private)构造函数的声明,以及无状态 session Bean 中任何其他非私有(private)无参数构造函数的不可用。据推断,您必须将 PersonaDAOImpl() 的可见性更改为公共(public)。

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:

关于java - 将 ejb 注入(inject)托管 bean 会导致 BeanInstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6642245/

相关文章:

java - 如何模拟java套接字连接中断?

java - 在 C++ 中是否有类似 Java 初始化 block 的构造?

java - Android - Acra 不报告

java - 远程EJB的理解

java - 对话范围 CDI 与 Ajax

java - Xamarin 中的错误 "option --boot-class-path not allowed with target 12"

java - 如何指示 maven-ejb-plugin 仅包含客户端 EJB 包中所需的类?

java - Hibernate SQL In 子句使 CPU 使用率达到 100%

hibernate - Bean 验证 (ConstraintValidator) + CDI + Tomcat 8 : HV000028: Unexpected exception during isValid call

java - Netbeans 上的 CDI @Target 警告消息