操屁眼的视频在线免费看,日本在线综合一区二区,久久在线观看免费视频,欧美日韩精品久久综

新聞資訊

    此文幾處有引用別人博客內容,如有侵權表示歉意!

    web項目中可能會遇到多數據源的情況,這個時候我們需要在不同的情況下使用不同的數據源對不同的數據庫進行訪問,最典型的案例是數據的讀寫分離。

    多數據源的設計思路:

    多數據源的具體配置步驟:

    配置文件中,配置不同的數據源

    
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.pwd}"/>
        bean>
    
    

    spring配置動態數據源_spring動態代理_spring aop動態代理

    <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url2}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.pwd}"/> bean>

    2.實現類 并繼承org..jdbc...ource類spring配置動態數據源,重寫upKey()方法:

    import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
    public class DynamicDataSource extends AbstractRoutingDataSource {
        @Override
        protected Object determineCurrentLookupKey() {
    

    spring aop動態代理_spring動態代理_spring配置動態數據源

    return DatabaseContextHolder.getCustomerType(); } }

    實現r類spring配置動態數據源,內部實現 類來保證線程安全

    public class DatabaseContextHolder {
        private static final ThreadLocal contextHolder = new ThreadLocal();
        public static void setCustomerType(String customerType) {contextHolder.set(customerType);}
        public static String getCustomerType() {return contextHolder.get();}
        public static void clearCustomerType() {contextHolder.remove();}
    }
    

    4.配置數據源集合

    spring aop動態代理_spring配置動態數據源_spring動態代理

    <bean id="dynamicDataSource" class="com.laie.pa.common.dao.DynamicDataSource">  
            <property name="targetDataSources">  
                <map key-type="java.lang.String">  
                    <entry value-ref="dataSource" key="dataSource">entry>  
                    <entry value-ref="dataSource2" key="dataSource2">entry>  
                map>  
            property>
           <!—配置默認數據源 -->  
            <property name="defaultTargetDataSource" ref="dataSource">property>  
        bean>
    

    配置數據源切換

    spring配置動態數據源_spring aop動態代理_spring動態代理

    5.1 實現r 類并配置Bean

    import org.aspectj.lang.JoinPoint;
    import org.springframework.stereotype.Component;
    @Component 
    public class DataSourceInterceptor {
        public void setdataSourceOne(JoinPoint jp) {
            DatabaseContextHolder.setCustomerType("dataSource");
        }
        public void setdataSourceTwo(JoinPoint jp) {
            DatabaseContextHolder.setCustomerType("dataSource2");
        }
    

    spring配置動態數據源_spring aop動態代理_spring動態代理

    }

    .xml 文件配置此Bean

    id="dataSourceInterceptor" class="com.laie.pa.common.dao.DataSourceInterceptor" />

    5.2 配置AOP 實現數據源切換

    <aop:config>  
            <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">  
                <aop:pointcut id="daoOne" expression="execution(* com.laie.pa.action.*.*(..))" />  
                <aop:pointcut id="daoTwo" expression="execution(* com.laie.pa.action2.*.*(..))" />  
                <aop:before pointcut-ref="daoOne" method="setdataSourceOne" />  
                <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" />  
            aop:aspect>  
        aop:config>   
    

    注:實現切換利用了aop中配置了執行的包下的所有方法使用不同的數據源。

網站首頁   |    關于我們   |    公司新聞   |    產品方案   |    用戶案例   |    售后服務   |    合作伙伴   |    人才招聘   |   

友情鏈接: 餐飲加盟

地址:北京市海淀區    電話:010-     郵箱:@126.com

備案號:冀ICP備2024067069號-3 北京科技有限公司版權所有