一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java注解實現(xiàn)動態(tài)數(shù)據(jù)源切換的實例代碼

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

2020-11-26 13:40aheizi Java教程

本篇文章主要介紹了Java注解實現(xiàn)動態(tài)數(shù)據(jù)源切換的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

實現(xiàn)原理

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

看下AbstractRoutingDataSource:

 

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

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean

 

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 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ù)這個key從resolvedDataSources這個map里取出對應(yīng)的DataSource,如果找不到,則用默認的resolvedDefaultDataSource。

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

使用方法

定義注解:

?
1
2
3
4
5
6
7
8
9
10
/**
 * Created by huangyangquan on 2016/11/30.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {
 
  DataSourceType value();
 
}

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

?
1
2
3
4
5
6
7
8
9
/**
 * Created by huangyangquan on 2016/11/30.
 */
public enum DataSourceType {
 
  MASTER,
  SLAVE
 
}

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

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

?
1
2
3
4
5
6
7
8
9
10
11
/**
 * 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使用了線程本地變量來保證線程安全。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * 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();
  }
 
}

我們在Spring的配置文件中配置數(shù)據(jù)源key值得對應(yīng)關(guān)系:

?
1
2
3
4
5
6
7
8
9
10
<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 表示主庫的從庫,是我們的兩個數(shù)據(jù)源。

接下來配置AOP切面:

?
1
2
3
4
5
6
7
8
9
<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" /><!-- 配置切點 -->
    <aop:before pointcut-ref="dataSourceCutPoint" method="before" />
  </aop:aspect>
</aop:config>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * 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)異常,手動設(shè)為主庫
      DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
    }
  }
 
}

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/aheizi/p/7071181.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美三级做爰全过程 | 99久久国产亚洲综合精品 | 国产成人影院 | 果冻传媒i91media免费 | hd性欧美俱乐部中文 | 日产2021免费一二三四区 | 美女尿口羞羞视频 | 亚洲精品一区二区三区在线播放 | 国产精品久久现线拍久青草 | 国产一级在线免费观看 | 日韩欧美在线看 | 国产激情视频在线 | 女教师被学生糟蹋三天 | 青草青青在线视频 | 91麻豆精品国产片在线观看 | 俄罗斯毛片免费大全 | 欧美极品摘花过程 | narutotsunade全彩雏田 | 亚洲AV精品一区二区三区不卡 | 欧美日韩在线成人看片a | 男人都懂www深夜免费网站 | 国语精彩对白2021 | 无人在线视频高清免费观看动漫 | 国产农村一级特黄α真人毛片 | 3p文两男一女办公室高h | 成人网欧美亚洲影视图片 | 国产馆| japanhd粗暴video | 美妇在男人胯下哀求 | 精品免费久久久久久影院 | 丝瓜茄子绿巨人秋葵榴莲污 | 精品国产一区二区三区久久久狼 | girlfriend动漫在线播放 | 日韩久久网 | 美女张开下身让男人桶 | 国产欧美一区二区三区精品 | 亚洲成人中文 | 日韩在线 中文字幕 | 免费aⅴ在线| 三体动漫在线观看免费完整版2022 | 国产欧美日韩免费一区二区 |