美文网首页
spring AOP配置读写分离

spring AOP配置读写分离

作者: wangxiaoda | 来源:发表于2017-02-23 16:30 被阅读60次

maven依赖引入

<dependency>
         <groupId>aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>1.5.3</version>
</dependency>
<dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version>1.8.9</version>
</dependency>

srping配置文件引入aop

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

spring配置文件添加

<aop:config expose-proxy="true">

      <aop:pointcut id="txPointcut" expression="execution(* com.keegoo..mapper..*.*(..))" />
      <aop:aspect ref="readWriteInterceptor" order="1">
          <!-- readOrWriteDB是ReadWriteInterceptor类里面的方法 -->
          <aop:around pointcut-ref="txPointcut" method="readOrWriteDB"/>
      </aop:aspect>

</aop:config>

<bean id="readWriteInterceptor" class="com.keegoo.interceptor.ReadWriteInterceptor">
      <!-- readMethodList是ReadWriteInterceptor类里面的方法 -->
      <property name="readMethodList">

          <list>
              <value>query*</value>
              <value>use*</value>
              <value>get*</value>
              <value>count*</value>
              <value>find*</value>
              <value>list*</value>
              <value>search*</value>
          </list>

      </property>
      <!-- writeMethodList是ReadWriteInterceptor类里面的方法 -->
      <property name="writeMethodList">
          <list>
              <value>save*</value>
              <value>add*</value>
              <value>create*</value>
              <value>insert*</value>
              <value>update*</value>
              <value>merge*</value>
              <value>del*</value>
              <value>remove*</value>
              <value>put*</value>
              <value>write*</value>
          </list>
      </property>

</bean>

ReadWriteInterceptor.java

package com.keegoo.interceptor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.PatternMatchUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangshuang on 17/2/23.
 */
public class ReadWriteInterceptor {

    private static final String DB_SERVICE = "dbService";
    private List<String> readMethodList = new ArrayList<String>();
    private List<String> writeMethodList = new ArrayList<String>();

    public Object readOrWriteDB(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        if (isChooseReadDB(methodName)) {
            //选择slave数据源
            System.out.print("==========================read");
        } else if (isChooseWriteDB(methodName)) {
            //选择master数据源
            System.out.print("==========================write");
        } else {
            //选择master数据源
            System.out.print("==========================else");
        }
        return pjp.proceed();
    }

    private boolean isChooseWriteDB(String methodName) {
        for (String mappedName : this.writeMethodList) {
            if (isMatch(methodName, mappedName)) {
                return true;
            }
        }
        return false;
    }

    private boolean isChooseReadDB(String methodName) {
        for (String mappedName : this.readMethodList) {
            if (isMatch(methodName, mappedName)) {
                return true;
            }
        }
        return false;
    }

    private boolean isMatch(String methodName, String mappedName) {
        return PatternMatchUtils.simpleMatch(mappedName, methodName);
    }

    public List<String> getReadMethodList() {
        return readMethodList;
    }

    public void setReadMethodList(List<String> readMethodList) {
        this.readMethodList = readMethodList;
    }

    public List<String> getWriteMethodList() {
        return writeMethodList;
    }

    public void setWriteMethodList(List<String> writeMethodList) {
        this.writeMethodList = writeMethodList;
    }

}


打印结果:

Paste_Image.png

相关文章

网友评论

      本文标题:spring AOP配置读写分离

      本文链接:https://www.haomeiwen.com/subject/wkhbwttx.html