Spring无法使用@PersistenceContext注入EntityManager

时间:2017-01-13 13:37:39

标签: java spring hibernate jpa wildfly

我有一个Java 8 / spring 4.3.5.RELEASE Web应用程序可在wildfly 10服务器上运行。我使用persistence.xml文件。我在jboss jpa和hibernate类上启用了跟踪日志记录,我可以看到这个文件被拾取并顺利解析为持久性单元:

DEBUG [] [org.hibernate.jpa.internal.util.LogHelper] PersistenceUnitInfo [
    name: testcontext
    persistence provider classname: org.hibernate.jpa.HibernatePersistenceProvider
    classloader: ModuleClassLoader for Module "deployment.BasicWebapp.war:main" from Service Module Loader
    excludeUnlistedClasses: false
    JTA datasource: org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@fb80232
    Non JTA datasource: null
    Transaction type: JTA
    PU root URL: vfs:/C:/Users/Me/Wildfly 10.0.0/standalone/deployments/BasicWebapp.war/WEB-INF/classes/
    Shared Cache Mode: UNSPECIFIED
    Validation Mode: AUTO
    Jar files URLs []
    Managed classes names [
        com.company.project.data.User]
    Mapping files names []
    Properties [
        jboss.entity.manager.jndi.name: persistence/testcontext]

我现在想要一个dao类让spring注入一个实体管理器:

UserDao.class

@Repository
public class UserDao
{
    @PersistenceContext(unitName = "testcontext")
    private EntityManager entityManager;
}

我有组件扫描和注释配置,因此在启动我的应用程序时会处理@Repository和@PersistenceContext注释:

弹簧servlet.xml中

<context:component-scan base-package="com.company.project" />
<context:annotation-config/>

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/testcontext"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>

<tx:annotation-driven/>

注射失败,但有两种可能的方式:

如果我使用@PersistenceContext(unitName = "testcontext"),则错误为: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testcontext' available

如果我使用@PersistenceContext,则错误为:

Caused by: java.lang.NullPointerException
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:580)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:546)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:707)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:680)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354)
    ... 44 more

所以我在这个配置中遗漏了一些东西,告诉Spring处理@PersistenceContext注释的部分,看看我在spring-servlet.xml中声明的entityManagerFactory bean,或者只是使用容器的持久性单元直接。为实现这一目标,我需要添加什么?

我在transactionmanager部分也有点粗略。 wildfly是否提供交易管理员?如果是的话,我是否需要在Spring中为它创建一个bean(它会选择jboss创建的bean还是自己创建一个bean?)

1 个答案:

答案 0 :(得分:0)

我认为您应该仔细检查一下针对Spring官方文档的配置设置:https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#orm-jpa-setup-jndi。否,事务管理器由Spring提供。有关此问题的更多信息:https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#transaction-strategies