数据源配置参数的datasource与persistence.xml

时间:2012-04-25 09:33:00

标签: spring jpa datasource persistence.xml

在我的项目中我使用spring和jpa,当我到达配置文件时,我指的是appContext.xml和persistence.xml,我遇到了以下情况:当我将db配置放在持久性中时。 xml和我从appContext.xml 无效中删除数据源但是当我将bd配置移动到appContext到dataSource标签(带有属性)时,应用程序正在运行,任何人都可以解释这个吗?提前谢谢!

Persistence.xml:

<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <class>ma.persistence.dto.Subscriber</class>
    <class>ma.persistence.dto.Profile</class>
    <class>ma.dto.Title</class>
</persistence-unit>
</persistence>

AppContext.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"  xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="ma.persistence"/>
<context:annotation-config />
<tx:annotation-driven />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver"  p:url="jdbc:mysql://localhost:3306/MyDB"
    p:username="username" p:password="password" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
    <property name="loadTimeWeaver">
        <bean
             class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform"
                value="org.hibernate.dialect.MySQLInnoDBDialect" /> 
        </bean>
    </property>
</bean>
<bean  id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="jpaAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
    p:database="MYSQL" p:showSql="true" />

0 个答案:

没有答案
相关问题