简体   繁体   中英

Read PropertyPlaceholderConfigurer in persistence.xml file

I know we can use spring's PropertyPlaceholderConfigurer bean in spring xml file which reads specified properties file and use values in xml file. Like wise is there a way where we can use this mechanism in my persistence.xml file.

Can i use org.eclipse.persistence.jpa.PersistenceProvider in datasource bean like this in spring xml file?

    <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>

Thanks in Advance.

Rather than using your build to create a prod or dev version of your persistence.xml, just move all property settings to your spring content.

read the original post by emeraldjava loading .properties in spring-context.xml and persistence.xml

Like I said in my comment, the first part is not possible, check this SO question

Concerning the second part: yes, that'll work. We use a separate datasource.xml file though and import it into the application context for better modularity.

spring-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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM