麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JSP > 正文

Spring AOP 動(dòng)態(tài)多數(shù)據(jù)源的實(shí)例詳解

2024-09-05 00:23:21
字體:
供稿:網(wǎng)友

 Spring AOP 動(dòng)態(tài)多數(shù)據(jù)源的實(shí)例詳解

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

 Spring,AOP,動(dòng)態(tài)多數(shù)據(jù)源,詳解

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

Spring,AOP,動(dòng)態(tài)多數(shù)據(jù)源,詳解

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

那么正確的做法應(yīng)該是:

Spring,AOP,動(dòng)態(tài)多數(shù)據(jù)源,詳解

具體代碼與配置如下:

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.xsdhttp://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 動(dòng)態(tài)數(shù)據(jù)源 -->  <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>  <!-- 多數(shù)據(jù)源 aop -->  <bean id="DataSourceAspect" class="com.carl.dbUtil.DataSourceAspect" />  <aop:config>     <aop:advisor pointcut="execution(* com.carl.o2o.mgr.*.*(..))" advice-ref="DataSourceAspect" />  </aop:config>   <!-- 事務(wù) -->  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">      <property name="dataSource" ref="dynamicDataSource"></property>  </bean> </beans>

2、DynamicDataSource

DynamicDataSource使用Spring中的代碼結(jié)合AOP實(shí)現(xiàn)多數(shù)據(jù)源切換.

public class DynamicDataSource extends AbstractRoutingDataSource {  public DynamicDataSource() {  }  protected Object determineCurrentLookupKey() {    return DBContextHolder.getDbType();  }  public Logger getParentLogger() {    return null;  }}

3、DBContextHolder

DynamicDataSource的輔助類,用于實(shí)際的切換多數(shù)據(jù)源。

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

多數(shù)據(jù)源AOP切面編程實(shí)現(xiàn)。

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 動(dòng)態(tài)多數(shù)據(jù)源的實(shí)例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關(guān)教程知識閱讀請移步到JSP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 成人电影毛片 | 久久亚洲网 | 亚洲码无人客一区二区三区 | 日本在线播放一区二区三区 | 欧美性成人 | 中文字幕观看 | 欧美一极视频 | 精品国产乱码久久久久久丨区2区 | 久久91久久久久麻豆精品 | 99在线热视频 | 一区二区久久精品66国产精品 | 日本一区二区三区视频在线 | av影院在线播放 | 日韩黄在线观看 | 毛片免费视频网站 | 天天草天天干天天射 | 国产精品久久久久久久四虎电影 | av在线免费观看国产 | 国产一区二区三区四区五区加勒比 | 国产精品成人av片免费看最爱 | 成年人网站视频免费 | 在线看免费观看av | 激情午夜天 | 国产精品视频一区二区三区四区国 | 99re热视频这里只精品 | 亚洲最新黄色网址 | 欧美性生交xxxxx久久久缅北 | 久久久久久免费 | 91精品国产乱码久久桃 | 91在线免费观看 | 精品久久久久久久久久久下田 | 成人毛片在线免费看 | 日韩欧美激情视频 | 毛片在线免费观看完整版 | hdbbwsexvideo | 91精品国产91久久久久久吃药 | 成人毛片100免费观看 | 一级免费在线视频 | 精品国产一区二区亚洲人成毛片 | 国产精品午夜未成人免费观看 | 免费激情网站 |