gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:20:27 24 4
gpt4 key购买 nike

我制作了一个简单的应用程序来测试我在小范围内遇到的这个问题。我有一个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


BeanInstantiationException 类型的异常通常在容器无法创建 bean 的实例时抛出。这很可能是由于声明了私有(private)构造函数,并且无状态 session Bean 中没有任何其他非私有(private)无参数构造函数。推断您将不得不更改 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:

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com