Autowire EntityManagerFactory注释为null

时间:2017-04-04 23:15:21

标签: java spring hibernate jpa

我正在将@PersistenceContext注入我的DAO类,如

@PersistenceContext
private EntityManager em;

@PersistenceContext是在我的项目中通过spring xml配置配置的。我正在尝试将以下spring xml配置转换为注释

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="com.company.myagentmonitor.model"
        p:dataSource-ref="companyDataSource"
        >
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

<!-- Transactions -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

我正在尝试将此配置转换为@Beans,我可以在我的DAO类中自动装配。我目前没有工作

@Bean
public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setDataSource(companyDataSource());
    entityManagerFactory.setPackagesToScan(new String[] { "com.company.myagentmonitor.model" });
    entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
    return entityManagerFactory.getObject();
}

@Bean
JpaTransactionManager transactionManager(final EntityManagerFactory emf) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory());
    return transactionManager;
}

@Bean
HibernateJpaVendorAdapter jpaVendorAdapter(){
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(false);
    jpaVendorAdapter.setShowSql(true);
    return jpaVendorAdapter;
}

我在运行时基本上得到一个空指针异常。我认为这与我创建或使用EntityManagerFactory的方式有关。

Caused by: java.lang.NullPointerException: null
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.initProxyClassLoa
der(SharedEntityManagerCreator.java:199) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.<init>(SharedEnti
tyManagerCreator.java:191) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.j
ava:163) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityMa
nager(PersistenceAnnotationBeanPostProcessor.java:719) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToIn
ject(PersistenceAnnotationBeanPostProcessor.java:680) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:
169) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-bea
ns-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(Persiste
nceAnnotationBeanPostProcessor.java:354) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        ... 64 common frames omitted

有人可以看看吗?不知道我做错了什么。谢谢!

2 个答案:

答案 0 :(得分:0)

首先,你不应该在getObject中调用entityManagerMethod()让Spring为你处理。只需返回LocalContainerEntityManagerFactoryBean

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setDataSource(companyDataSource());
    entityManagerFactory.setPackagesToScan(new String[] { "com.company.myagentmonitor.model" });
    entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
    return entityManagerFactory;
}

LocalContainerEntityManagerFactoryBean实现了几个弹簧检测到的接口,因此会为(init和destroy)注册回调,并注入一些所需的依赖项。

接下来需要EntityManagerFactory只需将其注入您的方法,而不是调用entityManagerFactory()

@Bean
JpaTransactionManager transactionManager(final EntityManagerFactory emf) {
    return new JpaTransactionManager(emf);
}

答案 1 :(得分:0)

更改此

@Bean
public EntityManagerFactory entityManagerFactory() {
    return entityManagerFactory.getObject();
}

到这个

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
     return entityManagerFactory;
}

并在您的transactionManager中也更改

transactionManager.setEntityManagerFactory(entityManagerFactory()); 

jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());