1. 程式人生 > >JPA配置多資料來源多persistence.xml檔案

JPA配置多資料來源多persistence.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    <!--開啟@Transactional註解 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- 注入bean @Component、@Controller、@Service、@Repository -->
    <!-- 注入beas層 -->
    <context:component-scan base-package="com.sunxl.base.controller" />
    <context:component-scan base-package="com.sunxl.base.service" />
    <context:component-scan base-package="com.sunxl.base.dao" />
    <!-- 注入前臺 -->
    <context:component-scan base-package="com.sunline.publics.controller" />
    <context:component-scan base-package="com.sunline.publics.service" />
    <context:component-scan base-package="com.sunline.publics.dao" />
    <!-- 注入後臺 -->
    <context:component-scan base-package="com.sunline.privates.controller" />
    <context:component-scan base-package="com.sunline.privates.service" />
    <context:component-scan base-package="com.sunline.privates.dao" />
    <!-- 測試注入 -->
    <context:component-scan base-package="com.sunxl.test.controller" />
    <context:component-scan base-package="com.sunxl.test.service" />
    <context:component-scan base-package="com.sunxl.test.dao" />
    <!-- 讀取的配置檔案 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations"><!-- 單個配置檔案使用location,且不用使用list,多個配置檔案使用locations -->
            <list>
                <value>classpath:/META-INF/system.properties</value>
                <value>classpath:/META-INF/dataSource.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8" />
    </bean>
    <!-- 配置事務異常封裝 -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <!-- 設定解析velocity的檢視 -->
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <value> resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
        </property>
    </bean>
    <!-- 啟動對@AspectJ註解的支援 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- 支援檔案圖片等的上傳 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
    <!--建立資料來源1,連線資料庫dataSource1 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${Mysql.sunxl.driverClass}" />
        <property name="jdbcUrl" value="${Mysql.sunxl.jdbcurl}" />
        <property name="user" value="${Mysql.sunxl.user}" />
        <property name="password" value="${Mysql.sunxl.password}" />
    </bean>
    <!--建立資料來源2,連線資料庫dataSource2 -->
    <bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${Mysql.test.driverClass}" />
        <property name="jdbcUrl" value="${Mysql.test.jdbcurl}" />
        <property name="user" value="${Mysql.test.user}" />
        <property name="password" value="${Mysql.test.password}" />
    </bean>
    <!-- 面向切面定義的類,可以在servcie之前做一定的操作-->
    <bean id="dataSourceAspect" class="com.sunxl.base.dao.handover.DataSourceAspect" />
    <!-- 面向切面攔截所有service方法 -->
    <aop:config>
        <aop:aspect ref="dataSourceAspect">
            <aop:pointcut id="dataSourcePointcut" expression="execution(* com.sunxl.*.service.*.*(..))" />
            <aop:before pointcut-ref="dataSourcePointcut" method="intercept" />
        </aop:aspect>
    </aop:config>
    <!-- persistenceProvider介面的一種實現非必須 -->
    <bean id="persistenceProvider" class="org.hibernate.ejb.HibernatePersistence" />
    <!-- 廠商JPA的一種特定規則非必須 -->
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="false" />
        <property name="database" value="HSQL" />
    </bean>
    <!-- JPA的特定規範 -->
    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    <!-- 生成實體工廠類 -->
    <!-- *************************************開始建立第一個工廠類*************************************** -->
    <!-- 定義第一個工廠類,工廠類預設使用的id名稱為entityManagerFactory,如果修改,其餘的使用都需要指定工廠名,如在web.xml中使用了 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="MYSQLONE" /><!-- jpa定義的名稱 -->
        <property name="persistenceXmlLocation" value="classpath:/META-INF/one-persistence.xml" /><!-- jpa定義的路徑 -->
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceProvider" ref="persistenceProvider" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" /><!--指定了如何獲取連線物件、開啟事務、關閉事務等事務管理相關的行為。  
-->
        <property name="jpaProperties"><!-- jpa配置 -->
            <props>
                <prop key="hibernate.dialect">${Mysql.sunxl.dialect}</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
                <prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
                <prop key="javax.persistence.query.timeout">20000</prop>
                <prop key="javax.persistence.lock.timeout">5000</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="net.sf.ehcache.configurationResourceName">classpath:/META-INF/one-ehcache.xml</prop>
                <prop key="hibernate.generate_statistics">false</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="jpa.showSql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!-- 開啟控制層和檢視層的延時載入功能,主要針對實體類中存在@ManyToMany(fetch = FetchType.LAZY)懶載入註解的實體類 -->
    <bean id="jpaInterceptor" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
        <property name="persistenceUnitName" value="MYSQLONE" />
    </bean>
    <!-- 配置事物管理,預設使用transactionManager,如果沒有,其餘的使用都需要指定事物 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <!-- *****************************************完成第一個工廠類************************************* -->
    <!-- *************************************開始建立第二個工廠類*************************************** -->
    <bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="MYSQLTWO" /><!-- jpa定義的名稱 -->
        <property name="persistenceXmlLocation" value="classpath:/META-INF/two-persistence.xml" /><!-- jpa定義的路徑 -->
        <property name="dataSource" ref="dataSource1" />
        <property name="persistenceProvider" ref="persistenceProvider" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
        <property name="jpaProperties"><!-- jpa配置 -->
            <props>
                <prop key="hibernate.dialect">${Mysql.sunxl.dialect}</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
                <prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
                <prop key="javax.persistence.query.timeout">20000</prop>
                <prop key="javax.persistence.lock.timeout">5000</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="net.sf.ehcache.configurationResourceName">classpath:/META-INF/two-ehcache.xml</prop>
                <prop key="hibernate.generate_statistics">false</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="jpa.showSql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!-- 開啟控制層和檢視層的延時載入功能,主要針對實體類中存在@ManyToMany(fetch = FetchType.LAZY)懶載入註解的實體類 -->
    <bean id="jpaInterceptor1" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
        <property name="persistenceUnitName" value="MYSQLONE" />
    </bean>
    <!-- 配置事物管理,預設使用transactionManager,如果沒有,其餘的使用都需要指定事物 -->
    <bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory1" />
    </bean>
    <!-- *****************************************完成第一個工廠類************************************* -->
    <!-- 開啟對映並注入延時載入 -->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="jpaInterceptor" />
                <ref bean="jpaInterceptor1" />
            </list>
        </property>
    </bean>
</beans>