Spring aop應用之實現資料庫讀寫分離,springaop

來源:互聯網
上載者:User

Spring aop應用之實現資料庫讀寫分離,springaop

    Spring加Mybatis實現MySQL資料庫主從讀寫分離 ,實現的原理是配置了多套資料來源,相應的sqlsessionfactory,transactionmanager和事務代理各配置了一套,如果從庫或資料庫有多個的時候,需要配置的資訊會越來越多,遠遠不夠優雅,在我們編程界有一個規    範:約定優於配置。所以就用Sping的aop實現了一個簡單的資料庫分離方案,具體實現代碼放在了Github上,地址如下:

    https://github.com/bridgeli/practical-util/tree/master/src/main/java/cn/bridgeli

讀者如果想使用再簡單的方法就是把這個代碼download下來,放到自己的項目裡面,當然更優雅的方式是:打成jar包,放到項目裡面了,具體打jar的方法,老夫就不在這裡多說了,相信看這篇文章的讀者肯定都會了。當然僅僅有這份代碼,他們是不會自動生效的,既然是使用Spring的Aop實現資料庫讀寫分離,
所以肯定會有牽涉到Aop的配置了,所以在spring-mybatis.xml中有如下配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"       xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <!-- 配置寫資料來源 -->    <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">        <property name="driverClassName" value="${bridgeli.jdbc.driver}" />        <property name="url" value="${bridgeli.jdbc.url}" />        <property name="username" value="${bridgeli.jdbc.username}" />        <property name="password" value="${bridgeli.jdbc.password}" />        <property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />        <property name="initialSize" value="1" />        <property name="maxActive" value="20" />        <property name="minIdle" value="0" />        <property name="maxWait" value="60000" />        <property name="testOnBorrow" value="false" />        <property name="testOnReturn" value="false" />        <property name="testWhileIdle" value="true" />        <property name="timeBetweenEvictionRunsMillis" value="60000" />        <property name="minEvictableIdleTimeMillis" value="25200000" />        <property name="removeAbandoned" value="true" />        <property name="removeAbandonedTimeout" value="1800" />        <property name="logAbandoned" value="true" />        <property name="filters" value="stat" />    </bean>    <!-- 配置讀資料來源 -->    <bean id="parentSlaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">        <property name="driverClassName" value="${bridgeli.jdbc.driver}" />        <property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />        <property name="initialSize" value="1" />        <property name="maxActive" value="20" />        <property name="minIdle" value="0" />        <property name="maxWait" value="60000" />        <property name="testOnBorrow" value="false" />        <property name="testOnReturn" value="false" />        <property name="testWhileIdle" value="true" />        <property name="timeBetweenEvictionRunsMillis" value="60000" />        <property name="minEvictableIdleTimeMillis" value="25200000" />        <property name="removeAbandoned" value="true" />        <property name="removeAbandonedTimeout" value="1800" />        <property name="logAbandoned" value="true" />        <property name="filters" value="stat" />    </bean>    <bean id="slaveDataSource1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="parentSlaveDataSource">        <property name="url" value="${bridgeli_slave1.jdbc.url}" />        <property name="username" value="${bridgeli_slave1.jdbc.username}" />        <property name="password" value="${bridgeli_slave1.jdbc.password}" />    </bean>    <bean id="dataSource" class="cn.bridgeli.datasource.MasterSlaveDataSource">        <property name="targetDataSources">            <map>                <entry key-ref="masterDataSource" value-ref="masterDataSource"/>                <entry key-ref="slaveDataSource1" value-ref="slaveDataSource1"/>            </map>        </property>        <property name="defaultTargetDataSource" ref="masterDataSource"/>        <property name="masterSlaveSelector" ref="dataSelector"/>    </bean>    <bean id="dataSelector" class="cn.bridgeli.datasource.MasterSlaveSelectorByPoll">        <property name="masters">            <list>                <ref bean="masterDataSource"/>            </list>        </property>        <property name="slaves">            <list>                <ref bean="slaveDataSource1"/>            </list>        </property>        <property name="defaultDataSource" ref="masterDataSource"/>    </bean>    <aop:aspectj-autoproxy/>    <!-- mybaits 資料處理站 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 自動掃描所有註解的路徑 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="cn.bridgeli.mapper" />        <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>    <!-- 資料庫切面 -->    <bean id="masterSlaveAspect" class="cn.bridgeli.datasource.MasterSlaveAspect">        <property name="prefixMasters">            <list>                <value>save</value>                <value>update</value>                <value>delete</value>            </list>        </property>    </bean>    <aop:config>        <aop:aspect id="c" ref="masterSlaveAspect">            <aop:pointcut id="tx" expression="execution(* cn.bridgeli.service..*.*(..))"/>            <aop:before pointcut-ref="tx" method="before"/>        </aop:aspect>    </aop:config>    <context:annotation-config />    <context:component-scan base-package="cn.bridgeli" /></beans>

 

這樣我們就很優雅的利用Spring的Aop實現了對資料庫的讀寫分離,讀的時候走slaveDataSource1這個資料來源,寫的時候走masterDataSource這個資料來源。
哎,等等,這裡哪裡體現了約定優於配置這一規範,他們怎麼知道哪些方法走讀庫哪些走寫庫?同學你別急,仔細讀讀這個設定檔,你就會發現在第98行,配置了一個MasterSlaveAspect,
也就是說代碼裡面service層(為什麼是service層?)的方法以這裡面配置的這些關鍵字打頭,都將會走寫庫,所以當我們想讓一個方法走主庫的時候,必須在這個地方添加該方法的首碼或者用這裡面已有的首碼,
這就要求我們必須約定好走主庫的方法的打頭,即約定優於配置。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.