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

aop

程序员文章站 2022-05-30 21:50:00
...
        <!-- springboot-aop包,AOP切面注解,Aspectd等相关注解 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>


package com.zy.ds;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


/**
* 自定义注解 + AOP的方式实现日志记录
* Created by linmz on 2020-05-12.
*/
@Aspect
@Component
public class OperateLogAspect {

    @Pointcut("execution(* com.zy.controller.UserCtr.*(..))")
    public void log(){}
@Before("log()")
    public void beforeSwitchDS(JoinPoint point){
        //获得当前访问的class
        Class<?> className = point.getTarget().getClass();
        //获得访问的方法名
        String methodName = point.getSignature().getName();
        //得到方法的参数的类型
        Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
        String dataSource = DataSourceContextHolder.DEFAULT_DS;
        try {
            // 得到访问的方法对象
            Method method = className.getMethod(methodName, argClass);
            // 判断是否存在@DS注解
            if (method.isAnnotationPresent(DS.class)) {
               //
               
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
   
    @After(value = "log()", returning="returnValue")
    public void afterSwitchDS(JoinPoint point, Object returnValue){
        String jsonStr = returnValue == null ? "": returnValue.toString();
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
       
    }

}