婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Spring AOP 動態多數據源的實例詳解

Spring AOP 動態多數據源的實例詳解

熱門標簽:如何看懂地圖標注點 海外圖書館地圖標注點 給地圖標注得傭金 電銷機器人免培訓 南通通訊外呼系統產品介紹 潤滑油銷售電銷機器人 外呼系統使用方法 自繪地圖標注數據 電話機器人需要使用網絡嗎

 Spring AOP 動態多數據源的實例詳解

當項目中使用到讀寫分離的時候,我們就會遇到多數據源的問題。多數據源讓人最頭痛的,不是配置多個數據源,而是如何能靈活動態的切換數據源。例如在一個spring和Mybatis的框架的項目中,我們在spring配置中往往是配置一個dataSource來連接數據庫,然后綁定給sessionFactory,在dao層代碼中再指定sessionFactory來進行數據庫操作。

 

正如上圖所示,每一塊都是指定綁死的,如果是多個數據源,也只能是下圖中那種方式。

可看出在Dao層代碼中寫死了兩個SessionFactory,這樣日后如果再多一個數據源,還要改代碼添加一個SessionFactory,顯然這并不符合開閉原則。

那么正確的做法應該是:

具體代碼與配置如下:

1、applicationContext-mgr.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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  !-- use annotation -->
  context:annotation-config />
    context:component-scan base-package="com.carl.o2o.**.mgr">
  /context:component-scan>

  !-- master -->
  bean id="master" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    property name="driverClass" value="${driverClassName_master}"/>
    property name="user" value="${username_master}"/>
    property name="password" value="${password_master}"/>
    property name="jdbcUrl" value="${url_master}?Unicode=truecharacterEncoding=UTF-8allowMultiQueries=true"/>

    property name="maxPoolSize" value="150"/> 
    property name="minPoolSize" value="10"/> 
    property name="initialPoolSize" value="20"/> 
    property name="maxIdleTime" value="3600"/> 
    property name="acquireIncrement" value="10"/> 
    property name="idleConnectionTestPeriod" value="1800"/>  
  /bean>

  !-- slave -->
  bean id="slave" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    property name="driverClass" value="${driverClassName_slave}"/>
    property name="user" value="${username_slave}"/>
    property name="password" value="${password_slave}"/>
    property name="jdbcUrl" value="${url_slave}?Unicode=truecharacterEncoding=UTF-8"/>

    property name="maxPoolSize" value="150"/> 
    property name="minPoolSize" value="10"/> 
    property name="initialPoolSize" value="20"/> 
    property name="maxIdleTime" value="3600"/> 
    property name="acquireIncrement" value="10"/> 
    property name="idleConnectionTestPeriod" value="1800"/>  
  /bean>

  !-- spring 動態數據源 -->
  bean id="dynamicDataSource" class="com.carl.dbUtil.DynamicDataSource">
    property name="targetDataSources"> 
      map key-type="java.lang.String"> 
        entry key="slave" value-ref="slave" /> 
      /map> 
    /property> 
    property name="defaultTargetDataSource" ref="master" />   
  /bean> 

  !-- mybatis mapper config -->
  bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    property name="dataSource" ref="dynamicDataSource"/>
    property name="configLocation" value="classpath:o2o_mybatis_config.xml"/>
    property name="mapperLocations" >
      list>
        value>classpath:sqlMap/*.xml/value>
        value>classpath*:/com/carl/o2o/**/*.xml/value>
      /list>
    /property>
  /bean>

  bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    constructor-arg index="0" ref="sqlSessionFactory">/constructor-arg>
  /bean>

  bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    property name="basePackage" value="com.carl.o2o.**.mgr.dao" />
  /bean>

  !-- 多數據源 aop -->
  bean id="DataSourceAspect" class="com.carl.dbUtil.DataSourceAspect" />
  aop:config> 
    aop:advisor pointcut="execution(* com.carl.o2o.mgr.*.*(..))" advice-ref="DataSourceAspect" />
  /aop:config> 

  !-- 事務 -->
  bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    property name="dataSource" ref="dynamicDataSource">/property>
  /bean>
 /beans>

2、DynamicDataSource

DynamicDataSource使用Spring中的代碼結合AOP實現多數據源切換.

public class DynamicDataSource extends AbstractRoutingDataSource {
  public DynamicDataSource() {
  }

  protected Object determineCurrentLookupKey() {
    return DBContextHolder.getDbType();
  }

  public Logger getParentLogger() {
    return null;
  }
}

3、DBContextHolder

DynamicDataSource的輔助類,用于實際的切換多數據源。

public class DBContextHolder {
  private static ThreadLocalString> contextHolder = new ThreadLocal();
  public static String MASTER = "master";
  public static String SLAVE = "slave";

  public DBContextHolder() {
  }

  public static String getDbType() {
    String db = (String)contextHolder.get();
    if(db == null) {
      db = MASTER;
    }

    return db;
  }

  public static void setDbType(String str) {
    contextHolder.set(str);
  }

  public static void setMaster() {
    contextHolder.set(MASTER);
  }

  public static void setSlave() {
    contextHolder.set(SLAVE);
  }

  public static void clearDBType() {
    contextHolder.remove();
  }
}

4、DataSourceAspect

多數據源AOP切面編程實現。

public class DataSourceAspect implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {
  private static final Logger log = LogManager.getLogger(DataSourceAspect.class);

  public DataSourceAspect() {
  }

  public void before(Method m, Object[] args, Object target) throws Throwable {
    try {
      if(m != null) {
        if((m.getName().startsWith("list") || m.getName().startsWith("select") || m.getName().startsWith("get") 
        || m.getName().startsWith("count"))  !m.getName().contains("FromMaster")) {
          DBContextHolder.setDbType("slave");
        } else {
          DBContextHolder.setDbType("master");
        }
      }
    } catch (Exception var5) {
      log.error("data source aspect error.", var5);
    }

  }

  public void after(JoinPoint point) {
    log.info("clear db type after method.current id {}", new Object[]{Long.valueOf(Thread.currentThread().getId())});
    DBContextHolder.clearDBType();
  }

  public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
  }

  public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
    log.info("current db type {} when exception", new Object[]{DBContextHolder.getDbType()});
    DBContextHolder.setDbType("master");
  }
}

以上就是 Spring AOP 動態多數據源的實例詳解,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • Spring+Mybatis 實現aop數據庫讀寫分離與多數據庫源配置操作
  • spring aop action中驗證用戶登錄狀態的實例代碼
  • 利用spring AOP記錄用戶操作日志的方法示例
  • 詳解SpringBoot AOP 攔截器(Aspect注解方式)
  • Spring AOP實現Redis緩存數據庫查詢源碼

標簽:內江 大連 黃石 南京 銅川 廣州 樂山 貸款邀約

巨人網絡通訊聲明:本文標題《Spring AOP 動態多數據源的實例詳解》,本文關鍵詞  Spring,AOP,動態,多,數據源,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Spring AOP 動態多數據源的實例詳解》相關的同類信息!
  • 本頁收集關于Spring AOP 動態多數據源的實例詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 团风县| 顺平县| 民和| 新竹市| 肥城市| 项城市| 莒南县| 家居| 乌拉特中旗| 渝北区| 怀宁县| 湖南省| 肇源县| 成安县| 谢通门县| 若尔盖县| 江陵县| 海原县| 普兰店市| 郁南县| 宝兴县| 枝江市| 云林县| 武平县| 土默特右旗| 昌邑市| 晋江市| 蒲城县| 广德县| 泾川县| 年辖:市辖区| 武威市| 石屏县| 文安县| 鲁甸县| 郁南县| 庆云县| 清徐县| 开封县| 内江市| 洛扎县|