spring boot + 自定义注解实现全局业务逻辑

注解基本知识

注解数据类型

注解是写在.java文件中,使用@interface作为关键字, 所以注解也是Java的一种数据类型,从广泛的定义来说,Class、Interface、Enum、Annotation都属于Class类型。

元注解

java中有四种元注解

@Documented 标记生成javadoc

@Inherited 标记继承关系

@Retention 注解的生存期

@Target 标注的目标

注解的保留策略
    @Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含,编译器在编译期使用,如@Override、@Deprecated等,这部分开发者应该使用的场景不多

  @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,这部分也很少见到

  @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

注解的作用目标(被放到上面位置,可以是多个):

  @Target(ElementType.TYPE)  // 接口、类、枚举、注解

  @Target(ElementType.FIELD)  // 字段、枚举的常量

  @Target(ElementType.METHOD)  // 方法

  @Target(ElementType.PARAMETER)  // 方法参数

  @Target(ElementType.CONSTRUCTOR)  // 构造函数

  @Target(ElementType.LOCAL_VARIABLE)  // 局部变量

  @Target(ElementType.ANNOTATION_TYPE) // 注解

  @Target(ElementType.PACKAGE)  // 包

注解包含在javadoc中:

  @Documented 

注解可以被继承:

  @Inherited

自定义注解

在重构的过程中,数据库层做读写分离扩展,通常的做法是根据方法名称,做统一的处理,query开头的,走只读库,update 、insert开头的走读写库,然而。。。。
由于之前快速开发代码规范不够,方法命名千奇百怪,无法通过这种方式实现,这时候,自定义注解就派上用场了

自定义注解 可以实现将添加注解的target(类、方法等)分类,然后做统一处理,虽然在命名上面没有可区分的共同点,但是我们可以用注解强行区分

下面用例子讲解一下
使用拦截器+注解实现读写分离(也可以是权限拦截)

注解定义:在method上使用,作用范围runtime可以反射获取到

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RWSwitch {
    DataSourceRouteKey  source() default DataSourceRouteKey.READWIRTE;
}


public enum DataSourceRouteKey {
    READONLY("readonly"),
    READWIRTE("readwrite");

    private String key;

    DataSourceRouteKey(String key){
        this.key = key;
    }

    public String key() {
        return key;
    }
}

controller

@RestController
public class AnnotationTestController {


    @RequestMapping("/read")
    @RWSwitch(source = DataSourceRouteKey.READONLY)
    public String test01(){
        System.out.println("test01 使用的数据源为: " + DynamicDataSourceHolder.getRouteKey());
        return "";
    }

    @RWSwitch
    @RequestMapping("/write")
    public String test02(){
        System.out.println("test02 使用的数据源为: " + DynamicDataSourceHolder.getRouteKey());
        return "";
    }

    @RWSwitch(source = DataSourceRouteKey.READWIRTE)
    @RequestMapping("/readWrite")
    public String test03(){
        System.out.println("test03 使用的数据源为: " + DynamicDataSourceHolder.getRouteKey());
        return "";
    }
}

拦截器

public class AnnotationInterceptor implements HandlerInterceptor {
    DynamicDataSourceHolder dynamicDataSourceHolder ;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        RWSwitch annotation = handlerMethod.getMethodAnnotation(RWSwitch.class);
        if(annotation==null){
            return true;
        }
        //如果当前数据源与注解不符,切换数据源
        dynamicDataSourceHolder.exchangeKey(annotation.source());
        System.out.println("method = "+handlerMethod.getMethod().getName()+"     key = "+annotation.source().key()+"          ds = "+DynamicDataSourceHolder.getRouteKey().key() );
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {

    }
}

数据源切换类

public class DynamicDataSourceHolder {
    private static ThreadLocal routeKey = new ThreadLocal();

    public static DataSourceRouteKey getRouteKey(){
        DataSourceRouteKey key = routeKey.get();
        return key;
    }

    public static void  setRouteKey(DataSourceRouteKey key)    {
        routeKey.set(key);
    }

    public static void removeRouteKey() {
        routeKey.remove();
    }
    public static void exchangeKey(DataSourceRouteKey dataSourceRouteKey){
        if(dataSourceRouteKey.equals(getRouteKey())){
            return;
        }else {
            System.out.println("exchange datasource to "+dataSourceRouteKey.key());
            setRouteKey(dataSourceRouteKey);
        }
    }
}

源码下载地址
https://gitee.com/gdtzbd/customizeannotation

你可能感兴趣的:(spring boot + 自定义注解实现全局业务逻辑)