有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java cdi不注入entitymanager

cdi不注入entitymanager,始终是空指针。以下配置:

坚持。xml

<?xml version="1.0" encoding="UTF-8"?>
 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="tutoroo" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/tutoroo</jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
    </persistence-unit>
 </persistence>

public class ProdutorEntityManager implements Serializable {

    private EntityManagerFactory factory = Persistence.createEntityManagerFactory("tutoroo");
   //private EntityManager entityManager = factory.createEntityManager();

    @Produces
    @PersistenceContext
    @RequestScoped
    public EntityManager criaEntityManager(){
        return factory.createEntityManager();
    }

    public void dispose(@Disposes EntityManager em) {
        em.close();
    }
}

public class UsuarioDaoImp implements UsuarioDao {

    @Inject
    private EntityManager manager; 

    public void salvar(Usuario usuario) {
        manager.persist(usuario);
    }
}

当我调试EntityManager UsuarioDaoImp类时,会出现以下异常:com。太阳jdi。调用方法时发生调用异常

我不知道我做错了什么。有人能帮忙吗

服务器是:jboss-as-7.1.1


共 (2) 个答案

  1. # 1 楼答案

    首先,不要自己在应用服务器中创建持久化单元,而是让服务器为您注入持久化单元

    以下是原因,来自JavaDocs

    The Persistence class is available in a Java EE container environment as well; however, support for the Java SE bootstrapping APIs is not required in container environments.

    不确定jbos-as-7的行为如何,但由于失去JTA支持等原因,通常不鼓励使用它

    为了简单起见,我假设您的应用程序中只有一个持久性单元。如果您需要具有多个持久性单元的应用程序的示例,请询问,我将进行编辑

    要在任何CDI托管bean中使用实体管理器,请执行以下操作:

    public class CDIBean {
    
      // the container injects it
      @PersistenceContext
      private EntityManager em;
    
      // just use it
      public void someMethod(Entity someEntity) {
        this.em.persist(someEntity);
      }
    
    }
    

    就这些

    然而,在许多例子中,出于各种原因宣布了生产者/处置者的组合。我打赌这就是困惑的根源。一些用例:

    • 允许您使用@Inject EntityManger em;而不是@PersistenceContext EntityManager em;

      // to make it available for injection using @Inject
      public class CDIProducer {
      
        // again, the container injects it
        @PersistenceContext
        private EntityManager em;
      
        // this will have the default dependent scope
        @Produces
        public EntityManager em() {
          return em;
        }
      
        public void dispose(@Disposes EntityManager em) {
          em.close();
        }
      
      }
      
      // to use it
      public class CDIBean {
      
        @Inject
        private EntityManager em;
      
        // just use it
        public void someMethod(Entity someEntity) {
          this.em.persist(someEntity);
        }
      
      }
      
    • 或者将实体管理器绑定到特定范围

      // to make it available for injection using @Inject, and bind it to the @RequestScope
      public class CDIProducer {
      
        // again, the container injects it
        @PersistenceContext
        private EntityManager em;
      
        // this will be in the request scope
        @Produces
        @RequestScoped
        public EntityManager em() {
          return em;
        }
      
        public void dispose(@Disposes @RequestScoped EntityManager em) {
          em.close();
        }
      
      }
      
      // to use it
      public class CDIBean {
      
        @Inject
        private EntityManager em;
      
        // just use it
        public void someMethod(Entity someEntity) {
          this.em.persist(someEntity);
        }
      
      }
      

    最后,可以将上述方法生产者转换为现场生产者。这相当于上一个示例:

    // to make it available for injection using @Inject, and bind it to the @RequestScope
    public class CDIProducer {
    
      @PersistenceContext
      @Produces
      @RequestScoped
      private EntityManager em;
    
      public void dispose(@Disposes @RequestScoped EntityManager em) {
        em.close();
      }
    
    }
    
  2. # 2 楼答案

    我认为@RequestScope不允许作为参数注入