欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring AOP 动态多数据源的实例详解

程序员文章站 2023-11-07 11:14:16
 spring aop 动态多数据源的实例详解 当项目中使用到读写分离的时候,我们就会遇到多数据源的问题。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵...

 spring aop 动态多数据源的实例详解

当项目中使用到读写分离的时候,我们就会遇到多数据源的问题。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。例如在一个spring和mybatis的框架的项目中,我们在spring配置中往往是配置一个datasource来连接数据库,然后绑定给sessionfactory,在dao层代码中再指定sessionfactory来进行数据库操作。

 Spring AOP 动态多数据源的实例详解

正如上图所示,每一块都是指定绑死的,如果是多个数据源,也只能是下图中那种方式。

Spring AOP 动态多数据源的实例详解

可看出在dao层代码中写死了两个sessionfactory,这样日后如果再多一个数据源,还要改代码添加一个sessionfactory,显然这并不符合开闭原则。

那么正确的做法应该是:

Spring AOP 动态多数据源的实例详解

具体代码与配置如下:

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=true&characterencoding=utf-8&allowmultiqueries=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=true&characterencoding=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 threadlocal<string> 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 动态多数据源的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!