spring如何检测当前的persistenceUnitName?

时间:2015-01-20 10:08:54

标签: spring jpa spring-data spring-data-jpa persistence.xml

我正在开发一个spring和JPA项目。我在Persistence.xml中配置了我的JPA持久性单元,这是我的spring配置文件。

我的应用程序工作正常,但我不明白spring框架如何检测我的Persistence.xml文件中定义的持久性单元并注入它而不在我的spring bean配置文件中定义。

有人可以回答我吗?

<?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:context="http://www.springframework.org/schema/context"
    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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="ma.professionalpartners.fireAppBusiness.dao"/> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="fireApp-Domain" />
    </bean>

    <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"  />
    </bean>

    <tx:annotation-driven transaction-manager="jpaTransactionManager" />

</beans>

2 个答案:

答案 0 :(得分:0)

在配置entityManagerFactory bean时,您已为持久性单元提供了名称:

<property name="persistenceUnitName" value="fireApp-Domain" />

答案 1 :(得分:0)

persistence.xml文件必须位于某些路径上,以便Spring只在这些位置搜索。找到文件后,它会解析XML内容,如果有一个PersistenceUnit,那么它就是默认的。当然,如果您指定一个名称(就像您所做的那样),那么它看起来就完全符合该PersistenceUnit。