SpringBoot Aop

SpringBoot Aop

Aop简介

面向切面编程,通过反射、代码织入实现在方法执行前、执行后调用切入函数,主要功能涉及日志记录,方法返回值处理、方法调用前资源准备等

切入点函数

1.execution
execution(方法修饰符(可选) 返回类型 方法名 参数 异常模式(可选))

方法通配符:
* 匹配任意字符,但是只能匹配一个元素
.. 匹配任意字符,可以匹配任意多个元素,表示类时,必须和*联合使用

+  必须跟在类名后面,如Horseman+,表示类本身和继承或扩展指定类的所有类

示例:
void chop(String,int)

匹配目标类任意修饰符方法、返回void、方法名chop、带有一个String和一个int型参数的方法

public void chop(*)

匹配目标类public修饰、返回void、方法名chop、带有一个任意类型参数的方法

public String *o*(..)

 匹配目标类public修饰、返回String类型、方法名中带有一个o字符、带有任意数量任意类型参数的方法

public void *o*(String,..)

 匹配目标类public修饰、返回void、方法名中带有一个o字符、带有任意数量任意类型参数,但第一个参数必须有且为String型的方法

也可以指定类:

public void examples.chap03.Horseman.*(..)

匹配Horseman的public修饰、返回void、不限方法名、带有任意数量任意类型参数的方法

public void examples.chap03.*man.*(..)

匹配以man结尾的类中public修饰、返回void、不限方法名、带有任意数量任意类型参数的方法

指定包:

public void examples.chap03.*.chop(..)

匹配examples.chap03包下所有类中public修饰、返回void、方法名chop、带有任意数量任意类型参数的方法

public void examples..*.chop(..)

匹配examples.包下和所有子包中的类中public修饰、返回void、方法名chop、带有任意数量任意类型参数的方法
2. @annotation()

表示标注了指定注解的目标类方法

例如 @annotation(org.springframework.transaction.annotation.Transactional) 表示标注了@Transactional的方法
3. args()

通过目标类方法的参数类型指定切点

例如 args(String) 表示有且仅有一个String型参数的方法
4. @args()

通过目标类参数的对象类型是否标注了指定注解指定切点

如 @args(org.springframework.stereotype.Service) 表示有且仅有一个标注了@Service的类参数的方法
5.within()

通过类名指定切点

如 with(examples.chap03.Horseman) 表示Horseman的所有方法
6. target()

通过类名指定,同时包含所有子类

如 target(examples.chap03.Horseman)  且Elephantman extends Horseman,则两个类的所有方法都匹配
7. @within()

匹配标注了指定注解的类及其所有子类

如 @within(org.springframework.stereotype.Service) 给Horseman加上@Service标注,则HorsemanElephantman 的所有方法都匹配
8. @target()

所有标注了指定注解的类

如 @target(org.springframework.stereotype.Service) 表示所有标注了@Service的类的所有方法
9. this()

大部分时候和target()相同,区别是this是在运行时生成代理类后,才判断代理类与指定的对象类型是否匹配

逻辑运算符

表达式可由多个切点函数通过逻辑运算组成

  1. &&

与操作,求交集,也可以写成and

例如 execution(* chop(..)) && target(Horseman) 表示Horseman及其子类的chop方法

  1. ||

或操作,求并集,也可以写成or

例如 execution(* chop(..)) || args(String) 表示名称为chop的方法或者有一个String型参数的方法

  1. !

非操作,求反集,也可以写成not

例如 execution(* chop(..)) and !args(String) 表示名称为chop的方法但是不能是只有一个String型参数的方法

注解说明

1. @Aspect
作用把当前类标识为一个切面供容器读取
2. @Before
标识一个前置增强方法,相当于BeforeAdvice,在切入点调用前进行调用
3. @AfterReturning
后置增强,相当于AfterReturningAdvice,切入点方法正常退出返回后执行
4.@AfterThrowing
后置异常,相当于ThrowsAdvice,切入点方法抛出异常执行
5.@After
final增强,不管是抛出异常或者正常退出都会执行
6.@Aroud
环绕增强,完整控制切入方法调用前调用后以及方法是否执行
7. @Order
对于多个切面对一个切入点来说,控制哪个切面先执行可以使用@Order注解,值越小越先执行

示例

方法调用前置换参数、日志记录,方法调用后结果统计

package com.chanjet.bigdata.aop;

import com.chanjet.bigdata.entity.ProducesData;
import com.chanjet.bigdata.model.AccountInfo;
import com.chanjet.bigdata.restful.RestfulTemplateUtil;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: pull_finance_data
 * @description: PullController aop配置 进行apply id置换  日志记录
 * @author: lipeng
 * @create: 2018-04-11 10:23
 **/
@Aspect
@Component
public class PullControllerAspect
{
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static final String ORG_ID = "orgId";

    private static final String PRODUCT_CD = "productCd";

    @Autowired
    private RestfulTemplateUtil restfulTemplateUtil;

    @Pointcut("execution( public * com.chanjet.bigdata.controller.PullController.*(..))")
    public void executeService()
    {

    }

    /**
     * 方法调用前 记录日志
     *
     * @param joinPoint
     * @return void
     * @author lipeng
     * @date 2018/4/11 15:19
     */
    @Before("executeService()")
    public void beforeInvokerInfoLog(JoinPoint joinPoint)
    {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Map<String, String[]> mapParams = request.getParameterMap();

        String method = joinPoint.getSignature().getName();
        StringBuffer sb = new StringBuffer();
        sb.append(method + " method be invoked,params is ");
        for (String key : mapParams.keySet())
        {
            sb.append(key + ":" + mapParams.get(key)[0] + " ");
        }
        logger.info(sb.toString());
    }

    /**
     * 目标方法返回后调用 记录查询到的条数
     *
     * @param joinPoint
     * @param result
     * @return void
     * @author lipeng
     * @date 2018/4/11 15:53
     */
    @AfterReturning(value = "executeService()", returning = "result")
    public void afterInvokerInfoLog(JoinPoint joinPoint, Object result)
    {
        int resultCount = 0;

        if(result == null)
        {
            return;
        }
        else if(result instanceof List)
        {
            resultCount = ((List) result).size();
        }
        else if(result instanceof Object[])
        {
            resultCount = ((Object[]) result).length;
        }
        else if(result instanceof ProducesData)
        {
            ProducesData producesData = (ProducesData) result;
            if(producesData.getDetails() != null)
            {
                resultCount = producesData.getDetails().size();
            }
        }
        else
        {
            return;
        }
        String method = joinPoint.getSignature().getName();
        logger.info(method + " method get result count: {}", resultCount);

    }

    /**
     * 环绕通知  接口调用前置换 org 参数
     *
     * @param proceedingJoinPoint
     * @return java.lang.Object
     * @author lipeng
     * @date 2018/4/11 14:44
     */
    @Around("executeService()")
    public Object applyIdAdapterOrgId(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //获取http参数集合
        Map<String, String[]> httpMapParams = request.getParameterMap();
        //获取被调用方法参数集合
        Map<String, Integer> methodParams = getMethodParams(proceedingJoinPoint);
        //获取调用参数
        Object[] args = proceedingJoinPoint.getArgs();
        //参数置换
        Object[] targetArgs = applyIdAdapterOrgId(httpMapParams, args, methodParams);
        //调用方法
        return proceedingJoinPoint.proceed(targetArgs);
    }


    /**
     * 申请id 置换 org id
     *
     * @param httpMapParams
     * @param targetArgs
     * @param methodParams
     * @return java.lang.Object[]
     * @author lipeng
     * @date 2018/4/17 10:36
     */
    private Object[] applyIdAdapterOrgId(Map<String, String[]> httpMapParams, Object[] targetArgs, Map<String, Integer> methodParams)
    {

        //调用参数中有org id需要进行判断  如果是请求id进行rest调用查询org id
        if(!httpMapParams.containsKey(ORG_ID))
        {
            return targetArgs;
        }

        String applyId = httpMapParams.get(ORG_ID)[0];

        //applyId 是空 或者小于15位
        if(StringUtils.isEmpty(applyId) || applyId.length() < 15)
        {
            return targetArgs;
        }
        //查询apply id对应的org id
        AccountInfo resultAdapter = restfulTemplateUtil.getAccountInfo(applyId);
        //未查询到对应的org product_cd信息
        if(resultAdapter == null || StringUtils.isEmpty(resultAdapter.getOrgId()))
        {
            return targetArgs;
        }
        logger.info("!apply id: {} substitution org id: {}", applyId, resultAdapter.getOrgId());

        if(methodParams.containsKey(ORG_ID))
        {
            targetArgs[methodParams.get(ORG_ID)] = resultAdapter.getOrgId();
        }
        //调用方法存在productCd参数
        if(methodParams.containsKey(PRODUCT_CD))
        {
            //请求未携带productCd 或 携带的productCd为空
            if(!httpMapParams.containsKey(PRODUCT_CD) ||
                    StringUtils.isEmpty(httpMapParams.get(PRODUCT_CD) != null ? httpMapParams.get(PRODUCT_CD)[0] : ""))
            {
                targetArgs[methodParams.get(PRODUCT_CD)] = resultAdapter.getLoanProductsChanjet();
            }
        }

        targetArgs[methodParams.get("bookId")] = resultAdapter.getLoanAccountBookId();
        return targetArgs;
    }

    /**
     * 方法参数与对应的索引映射到 map集合
     *
     * @param proceedingJoinPoint
     * @return java.util.Map<java.lang.String               ,               java.lang.Integer>
     * @author lipeng
     * @date 2018/4/17 10:30
     */
    private Map<String, Integer> getMethodParams(ProceedingJoinPoint proceedingJoinPoint)
    {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method invokedMethod = methodSignature.getMethod();
        Map<String, Integer> methodParamsName = new HashMap<>();
        //每个参数只有一个注解 二维数组列行为0
        Annotation[][] parameterAnnotations = invokedMethod.getParameterAnnotations();
        if(parameterAnnotations != null)
        {
            for (int i = 0, size = parameterAnnotations.length; i < size; i++)
            {
                Annotation annotation = parameterAnnotations[i][0];
                if(annotation instanceof RequestParam)
                {
                    //注解转换成 RequestParam
                    RequestParam requestParam = (RequestParam) annotation;
                    //方法名称 + index
                    methodParamsName.put(requestParam.value(), i);
                }
                else if(annotation instanceof AopParam)
                {
                    //注解转换成 RequestParam
                    AopParam requestParam = (AopParam) annotation;
                    //方法名称 + index
                    methodParamsName.put(requestParam.value(), i);
                }
            }
        }

        return methodParamsName;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值