在persistence.xml文件中读取PropertyPlaceholderConfigurer

时间:2012-01-13 07:03:36

标签: java spring spring-mvc persistence

我知道我们可以在spring xml文件中使用spring的 PropertyPlaceholderConfigurer bean,该文件读取指定的属性文件并使用xml文件中的值。同样明智的是我们可以在 persistence.xml 文件中使用此机制。

我可以在spring xml文件中的数据源bean中使用org.eclipse.persistence.jpa.PersistenceProvider吗?

    <bean id="dataSource"
    class="org.eclipse.persistence.jpa.PersistenceProvider">
    <property name="javax.persistence.jdbc.driver" value="${datasource.driverClassName}" />
    <property name="javax.persistence.jdbc.url" value="${datasource.url}" />
    <property name="javax.persistence.jdbc.user" value="${datasource.username}" />
    <property name="javax.persistence.jdbc.password" value="${datasource.password}" />
</bean>

<bean id="entityManager"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml"/>
    <property name="persistenceUnitName" value="JPAService"/>
    <property name="dataSource" ref="dataSource"/>

</bean>

先谢谢。

2 个答案:

答案 0 :(得分:0)

不要使用构建来创建persistence.xml的prod或dev版本,只需将所有属性设置移动到spring内容即可。

阅读emeraldjava loading .properties in spring-context.xml and persistence.xml

的原帖

答案 1 :(得分:0)

就像我在评论中说的那样,第一部分是不可能的,请查看SO question

关于第二部分:是的,那将是有效的。我们使用单独的datasource.xml文件并将其导入应用程序上下文以获得更好的模块化。

弹簧context.xml中:

<import resource="classpath:datasouce.xml" />

datasource.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                        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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd  
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-autowire="byName">

    <bean id="myDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="..." />
        <property name="password" value="..." />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myTestDB" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDatasource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
               <property name="showSql" value="true" />
               <property name="generateDdl" value="true" />
               <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            </bean>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

</beans>