使用spring初始化数据库读取,但是EntityManager值为null是什么原因?

时间:2021-07-25 17:28:22
本人没怎么用过spring,现在在移植一套大的代码,但是一直之后之前的数据库读取不能用了,具体表现在EntityManager的值一直为null,不知道是缺少了什么,还是哪里配错了,希望大家帮忙看下,谢谢:

datasource.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  <!-- Resolves variables with properties from "ds.properties".-->
  <bean class="com.intel.regist.action.CustomizedPropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
        <value>/WEB-INF/ds.properties</value>
        </list>
    </property>
  </bean>

  <bean id="dataSourceTsBase" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${datasource.driver.class.name}"/>
    <property name="url" value="${datasource.url}"/>
    <property name="username" value="${datasource.username}"/>
    <property name="password" value="${datasource.password}"/>
  </bean>

  <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="true"/>
    <property name="databasePlatform" value="${datasource.db.identifier}"/>
  </bean>

  <tx:annotation-driven/>

  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

  <bean id="entityManagerFactoryTsBase" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <qualifier value="ts-base"/>
    <property name="persistenceUnitName" value="name.test.tsbase"/>
    <property name="jpaVendorAdapter" ref="jpaAdapter"/>
    <property name="dataSource" ref="dataSourceTsBase"/>
  </bean>

  <bean id="transactionManagerTxn" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryTsBase"/>
    <qualifier value="ts-base"/>
  </bean>

  <bean id="sessionFactoryTsBase" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceTsBase"/>
  </bean>
</beans>

applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

  <!-- Includes the datasource used to implement the JPA-based database access. -->
  <import resource="datasource.xml"/>

</beans>

persistence.xml不确定应该放在什么目录下,目前在“ProjectName->Web Pages->META-INF”和"ProjectName->Configuration Files"目录下面都创建了一个:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
  <persistence-unit name="name.test.tsbase" transaction-type="RESOURCE_LOCAL"/>
</persistence>


数据库读取的那个类里面:
@PersistenceContext(unitName = "name.test.tsbase")
  private EntityManager em;

运行时进到这个类里面之后,断点看em的值为null。build和deploy到tomcat的时候也没有报错,无迹可寻,到底是哪里配错了呢?还是缺少类库?没有提示信息真是无从下手阿

2 个解决方案

#1


自己顶下,大家帮忙看看

#2


没人知道原因么?

#1


自己顶下,大家帮忙看看

#2


没人知道原因么?