最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java注解實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)例代碼

 更新時(shí)間:2017年06月30日 09:09:40   作者:aheizi  
本篇文章主要介紹了Java注解實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

當(dāng)一個(gè)項(xiàng)目中有多個(gè)數(shù)據(jù)源(也可以是主從庫)的時(shí)候,我們可以利用注解在mapper接口上標(biāo)注數(shù)據(jù)源,從而來實(shí)現(xiàn)多個(gè)數(shù)據(jù)源在運(yùn)行時(shí)的動(dòng)態(tài)切換。

實(shí)現(xiàn)原理

在Spring 2.0.1中引入了AbstractRoutingDataSource, 該類充當(dāng)了DataSource的路由中介, 能有在運(yùn)行時(shí), 根據(jù)某種key值來動(dòng)態(tài)切換到真正的DataSource上。

看下AbstractRoutingDataSource:

復(fù)制代碼 代碼如下:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean

AbstractRoutingDataSource繼承了AbstractDataSource,獲取數(shù)據(jù)源部分:

/** 
 * Retrieve the current target DataSource. Determines the 
 * {@link #determineCurrentLookupKey() current lookup key}, performs 
 * a lookup in the {@link #setTargetDataSources targetDataSources} map, 
 * falls back to the specified 
 * {@link #setDefaultTargetDataSource default target DataSource} if necessary. 
 * @see #determineCurrentLookupKey() 
 */ 
protected DataSource determineTargetDataSource() { 
  Assert.notNull(this.resolvedDataSources, "DataSource router not initialized"); 
  Object lookupKey = determineCurrentLookupKey(); 
  DataSource dataSource = this.resolvedDataSources.get(lookupKey); 
  if (dataSource == null && (this.lenientFallback || lookupKey == null)) { 
    dataSource = this.resolvedDefaultDataSource; 
  } 
  if (dataSource == null) { 
    throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]"); 
  } 
  return dataSource; 
}

抽象方法 determineCurrentLookupKey() 返回DataSource的key值,然后根據(jù)這個(gè)key從resolvedDataSources這個(gè)map里取出對(duì)應(yīng)的DataSource,如果找不到,則用默認(rèn)的resolvedDefaultDataSource。

我們要做的就是實(shí)現(xiàn)抽象方法 determineCurrentLookupKey() 返回?cái)?shù)據(jù)源的key值。

使用方法

定義注解:

/**
 * Created by huangyangquan on 2016/11/30.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {

  DataSourceType value();

}

注解為數(shù)據(jù)源的名稱,可定義一個(gè)枚舉類表示:

/**
 * Created by huangyangquan on 2016/11/30.
 */
public enum DataSourceType {

  MASTER,
  SLAVE

}

注解定義好了,我們利用Spring的AOP根據(jù)注解內(nèi)容對(duì)數(shù)據(jù)源進(jìn)行選擇,這里需要利用上面提到的 AbstractRoutingDataSource 類,該類是能夠?qū)崿F(xiàn)數(shù)據(jù)源切換的關(guān)鍵所在。

定義類DynamicDataSource繼承AbstractRoutingDataSource,并實(shí)現(xiàn) determineCurrentLookupKey() ,返回?cái)?shù)據(jù)源的key值。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

  @Override
  protected Object determineCurrentLookupKey() {
    return DynamicDataSourceHolder.getDataSourceType();
  }

}

 DynamicDataSourceHolder 是我們管理DataSource的類,將一次數(shù)據(jù)庫操作的數(shù)據(jù)源名稱保存在DynamicDataSourceHolder中,以供后面的操作在此context中取數(shù)據(jù)源key,其中DataSourceType使用了線程本地變量來保證線程安全。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DynamicDataSourceHolder {

  // 線程本地環(huán)境
  private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<DataSourceType>();

  // 設(shè)置數(shù)據(jù)源類型
  public static void setDataSourceType(DataSourceType dataSourceType) {
    Assert.notNull(dataSourceType, "DataSourceType cannot be null");
    contextHolder.set(dataSourceType);
  }

  // 獲取數(shù)據(jù)源類型
  public static DataSourceType getDataSourceType() {
    return (DataSourceType) contextHolder.get();
  }

  // 清除數(shù)據(jù)源類型
  public static void clearDataSourceType() {
    contextHolder.remove();
  }

}

我們?cè)赟pring的配置文件中配置數(shù)據(jù)源key值得對(duì)應(yīng)關(guān)系:

<bean id="spyGhotelDataSource" class="com.aheizi.config.DynamicDataSource">
  <property name="targetDataSources">
    <map key-type="java.lang.String">
      <entry key="MASTER" value-ref="TEST-MASTER-DB"></entry>
      <entry key="SLAVE" value-ref="TEST-SLAVE-DB"></entry>
    </map>
  </property>
  <property name="defaultTargetDataSource" ref="TEST-MASTER-DB">
  </property>
</bean>

設(shè)置targetDataSources和defaultTargetDataSource。 TEST-MASTER-DB TEST-SLAVE-DB 表示主庫的從庫,是我們的兩個(gè)數(shù)據(jù)源。

接下來配置AOP切面:

<aop:aspectj-autoproxy proxy-target-class="false" />
<bean id="manyDataSourceAspect" class="com.aheizi.config.DataSourceAspect" />
<aop:config>
  <aop:aspect id="dataSourceCut" ref="manyDataSourceAspect">
    <aop:pointcut expression="execution(* com.aheizi.dao.*.*(..))"
      id="dataSourceCutPoint" /><!-- 配置切點(diǎn) -->
    <aop:before pointcut-ref="dataSourceCutPoint" method="before" />
  </aop:aspect>
</aop:config>

以下是切面中before執(zhí)行的DataSourceAspect的實(shí)現(xiàn),主要實(shí)現(xiàn)的功能是獲取方法上的注解,根據(jù)注解名稱將值設(shè)置到DynamicDataSourceHolder中,這樣在執(zhí)行查詢的時(shí)候, determineCurrentLookupKey() 返回?cái)?shù)據(jù)源的key值就是我們希望的那個(gè)數(shù)據(jù)源了。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DataSourceAspect {

  private static final Logger LOG = LoggerFactory.getLogger(DataSourceAspect.class);

  public void before(JoinPoint point){
    Object target = point.getTarget();
    String method = point.getSignature().getName();
    Class<?>[] classz = target.getClass().getInterfaces();
    Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
    try {
      Method m = classz[0].getMethod(method, parameterTypes);
      if (m != null && m.isAnnotationPresent(DataSource.class)) {
        // 訪問mapper中的注解
        DataSource data = m.getAnnotation(DataSource.class);
        switch (data.value()) {
          case MASTER:
            DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
            LOG.info("using dataSource:{}", DataSourceType.MASTER);
            break;
          case SLAVE:
            DynamicDataSourceHolder.setDataSourceType(DataSourceType.SLAVE);
            LOG.info("using dataSource:{}", DataSourceType.SLAVE);
            break;
        }
      }
    } catch (Exception e) {
      LOG.error("dataSource annotation error:{}", e.getMessage());
      // 若出現(xiàn)異常,手動(dòng)設(shè)為主庫
      DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
    }
  }

}

這樣我們就實(shí)現(xiàn)了一個(gè)動(dòng)態(tài)數(shù)據(jù)源切換的功能。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JAVA用遞歸實(shí)現(xiàn)全排列算法的示例代碼

    JAVA用遞歸實(shí)現(xiàn)全排列算法的示例代碼

    這篇文章主要介紹了JAVA用遞歸實(shí)現(xiàn)全排列算法的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 使用Spring Security集成手機(jī)驗(yàn)證碼登錄功能實(shí)現(xiàn)

    使用Spring Security集成手機(jī)驗(yàn)證碼登錄功能實(shí)現(xiàn)

    本文詳細(xì)介紹了如何利用SpringSecurity來實(shí)現(xiàn)手機(jī)驗(yàn)證碼的注冊(cè)和登錄功能,在登錄過程中,同樣需通過驗(yàn)證碼進(jìn)行驗(yàn)證,文章還提供了相關(guān)的代碼實(shí)現(xiàn)
    2024-10-10
  • java開發(fā)中常遇到的各種難點(diǎn)以及解決思路方案

    java開發(fā)中常遇到的各種難點(diǎn)以及解決思路方案

    Java項(xiàng)目是一個(gè)復(fù)雜的軟件開發(fā)過程,其中會(huì)涉及到很多技術(shù)難點(diǎn),這篇文章主要給大家介紹了關(guān)于java開發(fā)中常遇到的各種難點(diǎn)以及解決思路方案的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • Java框架Struts2實(shí)現(xiàn)圖片上傳功能

    Java框架Struts2實(shí)現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • IDEA連接mysql報(bào)錯(cuò)的問題及解決方法

    IDEA連接mysql報(bào)錯(cuò)的問題及解決方法

    這篇文章主要介紹了IDEA連接mysql報(bào)錯(cuò)的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 使用Feign?logging?開啟調(diào)用日志

    使用Feign?logging?開啟調(diào)用日志

    這篇文章主要介紹了使用Feign?logging?開啟調(diào)用日志,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 小議Java中final關(guān)鍵字使用時(shí)的注意點(diǎn)

    小議Java中final關(guān)鍵字使用時(shí)的注意點(diǎn)

    final關(guān)鍵字代表著最后、不可改變,無論是在用final修飾類、修飾方法還是修飾變量時(shí),都要注意內(nèi)存分配的問題.這里來小議Java中final關(guān)鍵字使用時(shí)的注意點(diǎn):
    2016-06-06
  • Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊(duì)列詳解

    Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊(duì)列詳解

    這篇文章主要介紹了Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊(duì)列詳解,棧(Stack) 是一種基本的數(shù)據(jù)結(jié)構(gòu),具有后進(jìn)先出(LIFO)的特性,類似于現(xiàn)實(shí)生活中的一疊盤子,棧用于存儲(chǔ)一組元素,但只允許在棧頂進(jìn)行插入(入棧)和刪除(出棧)操作,需要的朋友可以參考下
    2023-10-10
  • 一文搞懂MyBatis多數(shù)據(jù)源Starter實(shí)現(xiàn)

    一文搞懂MyBatis多數(shù)據(jù)源Starter實(shí)現(xiàn)

    本文將實(shí)現(xiàn)一個(gè)MyBatis的Springboot的Starter包,引用這個(gè)Starter包后,僅需要提供少量配置信息,就能夠完成MyBatis多數(shù)據(jù)源的初始化和使用,需要的小伙伴可以參考一下
    2023-04-04
  • Java中IO流 RandomAccessFile類實(shí)例詳解

    Java中IO流 RandomAccessFile類實(shí)例詳解

    這篇文章主要介紹了Java中IO流 RandomAccessFile類實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評(píng)論

贵港市| 福州市| 洮南市| 邹平县| 赣榆县| 利辛县| 遂平县| 镇江市| 贺兰县| 昌邑市| 磴口县| 武宁县| 泌阳县| 镇远县| 西青区| 石屏县| 雷州市| 屏东市| 西畴县| 阿图什市| 汉沽区| 肃宁县| 惠安县| 金平| 将乐县| 广汉市| 师宗县| 岑巩县| 科技| 湟中县| 衡南县| 儋州市| 云和县| 察哈| 雷波县| 沁阳市| 通城县| 肇东市| 天柱县| 伊金霍洛旗| 建德市|