EJB 3.1依赖注入失败

我已经创建了一个无状态会话bean,如下所示:

@WebServlet(name = "ProductController", urlPatterns = {"/ProductController"})
public class ProductController extends HttpServlet {

  @EJB
  private ProductFacadeBean productBean;
}

@Stateless
public class ProductFacadeBean extends AbstractFacade<Product> implements ProductFacadeLocalInterface {
  @PersistenceContext(unitName = "OnlineStorePU")
  private EntityManager em;

  protected EntityManager getEntityManager() {
    return em;
  }

  public ProductFacadeBean() {
    super(Product.class);
  }

}

@Local
public interface ProductFacadeLocalInterface {

  void create(Product product);

  void edit(Product product);

  void remove(Product product);

  Product find(Object id);

  List<Product> findAll();

  List<Product> findRange(int[] range);

  int count();

}


public abstract class AbstractFacade<T> {
  private Class<T> entityClass;

  public AbstractFacade(Class<T> entityClass) {
    this.entityClass = entityClass;
  }

  protected abstract EntityManager getEntityManager();

  public void create(T entity) {
    getEntityManager().persist(entity);
  }

  public void edit(T entity) {
    getEntityManager().merge(entity);
  }

  public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
  }

  public T find(Object id) {
    return getEntityManager().find(entityClass, id);
  }

  public List<T> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return getEntityManager().createQuery(cq).getResultList();
  }

  public List<T> findRange(int[] range) {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
  }

  public int count() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
    cq.select(getEntityManager().getCriteriaBuilder().count(rt));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
  }

}

题 :

  • 什么是错误? 如何解决它?

    javax.naming.NamingException:在SerialContext中'java:comp / env / Controller.ProductController / productBean'的查找失败[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java。 naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,java.naming.factory.url.pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NamingException:为远程ejb-ref名称= Controller.ProductController / productBean解析Ejb异常,远程3.x接口= EJB.ProductFacadeBean,ejb-link = null,lookup =,mappedName =,jndi-name = EJB.ProductFacadeBean,refType = Session '。 用于查找的实际(可能内部的)远程JNDI名称是'EJB.ProductFacadeBean#EJB.ProductFacadeBean'[根异常是javax.naming.NamingException:在SerialContext中'EJB.ProductFacadeBean#EJB.ProductFacadeBean'的查找失败[myEnv = {java .naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,java.naming.factory.url .pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NameNotFoundException:EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]]

  • 完整的例外情况附于此处:

    HTTP Status 500 -
    
    type Exception report
    
    message
    
    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
    
    exception
    
    javax.servlet.ServletException: PWC1392: Error instantiating servlet class Controller.ProductController
    
    root cause
    
    com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class Controller.ProductController
    
    root cause
    
    com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=Controller.ProductController/productBean,Remote
    3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session into class Controller.ProductController
    
    root cause
    
    javax.naming.NamingException: Lookup failed for 'java:comp/env/Controller.ProductController/productBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=Controller.ProductController/productBean,Remote
    3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [Root exception is javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]]
    
    root cause
    
    javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=Controller.ProductController/productBean,Remote
    3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [Root exception is javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]
    
    root cause
    
    javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]
    
    root cause
    
    javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found
    
    note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1 logs. GlassFish Server Open Source Edition 3.1
    
  • 当我使用netbeans为实体bean创建会话时,它为我们创建了抽象外观? 什么目的 ? 我知道门面模式是作为路由请求的接口吗?
  • ejb-jar.xml有什么用?
  • 所有设置都是默认设置 ejb bean是在java ee 6 web应用程序内创建的,而不是在另一个war或jar中创建的。 Glassfish 3.1服务器。

    请帮忙。

    谢谢。


    你必须使用界面。 如果你使用Seam SolderCDI你可以指定具体的实现

    @Inject
    @Exact(ProductFacadeBean.class)
    

    您需要通过引用它的接口来注入bean,而不是直接引用它的实现。 像这样的东西..

    @EJB
    private ProductFacadeLocalInterface productBean;
    

    然后您可以使用您在ProductFacadeLocalInterface公开的接口方法访问该bean。

    链接地址: http://www.djcxy.com/p/58771.html

    上一篇: EJB 3.1 Dependency Injection Failed

    下一篇: How to represent and track mutable state in a text