【发布时间】:2014-07-24 01:37:22
【问题描述】:
我正在尝试使用 Spring 4 设置实体管理器,当我尝试使用 @PersistenceContext 注释注入 EntityManager 时,我总是得到 NullPointerException。
我有 Maven 网络应用程序。这是我的 applicationContext.xml 配置
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="EcommercePU">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mysite.ecommerceapp.domain</value>
<value>com.mysite.ecommerceapp.domain.*</value>
<value>com.mysite.ecommerceapp.domain.*.*</value>
<value>com.mysite.ecommerceapp.domain.*.*.*</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/ecommercedb" />
<property name="username" value="postgres" />
<property name="password" value="test" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
applicationContext.xml 位于 WEB-INF 文件夹下。因为我使用 NetBeans 8,所以我从项目属性 -> 框架中为我的项目添加了 Spring Framework 支持。我正在使用 EntityManager 注入和名为 GenericDaoImpl 的文件,这是它的代码:
public abstract class GenericDaoImpl<E extends EntityClass> implements GenericDao<E> {
private Class<E> entityClass;
@PersistenceContext()
private EntityManager entityManager;
//Constructor
public GenericDaoImpl(final Class<E> entityClass) {
this.entityClass = entityClass;
}
//Getters and Setters
public Class<E> getEntityClass() {
return entityClass;
}
public void setEntityClass(final Class<E> entityClass) {
this.entityClass = entityClass;
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(final EntityManager entityManager) {
this.entityManager = entityManager;
}
public void checkEntityManager() {
System.out.println("em: " + this.entityManager);
}
}
我有一个测试文件,我只需使用 checkEntityManager() 方法打印实体管理器。每当我运行它时,我总是得到 EntityManager 的 null 值。有什么问题?我最大的疑问是我的 applicationContext.xml 放错了地方,或者它没有在任何地方使用,因此在 applicationContext 中找不到配置属性。我已经测试过将 applicationContext.xml 文件移动到资源文件夹,但它也没有帮助。我还有其他问题如下:
我需要有 persistence.xml 吗?由于我使用带有 packagesToScan 功能的 Spring 4,我听说persistence.xml 不是必需的,但我不确定。
使用 hibernate 只处理基本实体 crud 和其他带有 JdbcTemplate 的 dao 查询是否明智?
我们将不胜感激。
【问题讨论】:
-
如何创建扩展
GenericDaoImpl的子类的实例(由new创建,让spring 注入它们(@Autowired)或每个context.xml)? -
嗨拉尔夫。我用
new关键字实例化子类。但我根本没有使用过@Autowired。除了以下行<Context antiJARLocking="true" path="/ECommerceApp"/>之外,我在 context.xml 文件中没有其他任何内容 -
如果使用new创建实例,Spring无从知晓,无法注入实体管理器。您必须在另一个 Spring bean 中自动装配它们。
-
嗨,JB,但为了自动装配,我不应该在 context.xml 文件中添加所有可自动装配的类吗?另一件事是,当我有一个扩展上面的 GenericDaoImpl 的 dao 类时,我用
new关键字实例化它并测试 EntityManager 它仍然给我一个NullPointerException。
标签: java spring hibernate web-applications entitymanager