使用spring通過aop獲取方法參數和參數值

spring通過aop獲取方法參數和參數值

自定義註解

package com.xiaolc.aspect;  
import java.lang.annotation.*; 
/**
 * @author lc
 * @date 2019/9/10
 */
@Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LiCheng {
}

切面

package com.xiaolc.aspect; 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 獲取方法上的註解值
 */
@Component
@Aspect
public class AuditAnnotationAspect {
 
    @Around("@annotation(liCheng))")
    private static Map getFieldsName(ProceedingJoinPoint joinPoint,LiCheng liCheng) throws ClassNotFoundException, NoSuchMethodException {
        String classType = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        // 參數值
        Object[] args = joinPoint.getArgs();
        Class<?>[] classes = new Class[args.length];
        for (int k = 0; k < args.length; k++) {
            if (!args[k].getClass().isPrimitive()) {
                // 獲取的是封裝類型而不是基礎類型
                String result = args[k].getClass().getName();
                Class s = map.get(result);
                classes[k] = s == null ? args[k].getClass() : s;
            }
        }
        ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
        // 獲取指定的方法,第二個參數可以不傳,但是為瞭防止有重載的現象,還是需要傳入參數的類型
        Method method = Class.forName(classType).getMethod(methodName, classes);
        // 參數名
        String[] parameterNames = pnd.getParameterNames(method);
        // 通過map封裝參數和參數值
        HashMap<String, Object> paramMap = new HashMap();
        for (int i = 0; i < parameterNames.length; i++) {
            paramMap.put(parameterNames[i], args[i]);
            System.out.println("參數名:"+parameterNames[i]+"\n參數值"+args[i]);
        }
        return paramMap;
    }
    private static HashMap<String, Class> map = new HashMap<String, Class>() {
        {
            put("java.lang.Integer", int.class);
            put("java.lang.Double", double.class);
            put("java.lang.Float", float.class);
            put("java.lang.Long", Long.class);
            put("java.lang.Short", short.class);
            put("java.lang.Boolean", boolean.class);
            put("java.lang.Char", char.class);
        }
    };
}

aop切面 註解、參數獲取

在工作中會經常使用aop,這裡將aop使用基本方法,獲取在切點中使用的獲取參數、註解做一個樣例。

1、定義需要切面的註解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnnDemo {
    String value();
    boolean isAop() default true;
}

2、在需要進行切面的方法標註註解

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/all")
    @AnnDemo(value = "all",isAop = false)
    public List<TbOrder> findAll() {
        List<TbOrder> list = orderService.getOrderList();
        return list;
    }
    @RequestMapping("/page")
    @AnnDemo(value = "page")
    public List<TbOrder> findPage(@RequestParam("username") String username) {
        List<TbOrder> listPage = orderService.getOrdersListPage();
        return listPage;
    }
}

3、定義切面

在切面中獲取切點註解,方法,參數的獲取

@Aspect
@Component
public class AspectDemo {
    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))")
    public void excetionMethod() {}
    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
    public void excetionNote() { }
    @Before("excetionMethod()")
    public void testBefore(JoinPoint joinPoint) {
        System.out.println("----------------------------前置通知---");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
    }
    @Around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
    public Object  testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {
        //用的最多通知的簽名
        Signature signature = joinPoint.getSignature();
        MethodSignature msg=(MethodSignature) signature;
        Object target = joinPoint.getTarget();
        //獲取註解標註的方法
        Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());
        //通過方法獲取註解
        AnnDemo annotation = method.getAnnotation(AnnDemo.class);
        Object proceed;
        //獲取參數
        Object[] args = joinPoint.getArgs();
        System.out.println(annotation.value());
        System.out.println(annotation.isAop());
        for (Object arg : args) {
            System.out.println(arg);
        }
        if (Objects.isNull(annotation) || !annotation.isAop()) {
            System.out.println("無需處理");
            proceed = joinPoint.proceed();
        }else {
            System.out.println("進入aop判斷");
            proceed = joinPoint.proceed();
            if(proceed instanceof List){
                List proceedLst = (List) proceed;
                if(!CollectionUtils.isEmpty(proceedLst)){
                    TbOrder tbOrder = new TbOrder();
                    tbOrder.setPaymentType("fffffffffffffffffff");
                    ArrayList<TbOrder> tbOrderLst = new ArrayList<>();
                    tbOrderLst.add(tbOrder);
                    return tbOrderLst;
                }
            }
            System.out.println(proceed);
        }
        return proceed;
    }
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: