Springboot使用AOP

spring-boot-aop–面向切面编程

  • spring-boot-starter-aop 支持AOP
  • aspectjtools 使用aspectj实现
一、快速开始
1、添加依赖
<!-- aop依赖 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjtools</artifactId>
	<version>1.8.0</version>
</dependency>
2、编写AOP切面处理类
@Aspect
@Component
public class MyAspect {

    //=============切点====================//
    //方式一:使用注解的方式 方式二:使用EL表示的是包形式
	@Pointcut("@annotation(com.springboot.annotation.Log)")
    //@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")
	public void pointcut() { }

    //======================通知=================//
    //环绕通知
	@Around("pointcut()")
	public void around(ProceedingJoinPoint point) {
		// do AOP methods
	}
    //前置通知
    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) {
        LOGGER.info("doAfter():{}", joinPoint.toString());
    }
    //后置通知
    @After("pointcut()")
    public void doAfter(JoinPoint joinPoint) {
        LOGGER.info("doAfter():{}", joinPoint.toString());
    }
	//后置返回通知
    @AfterReturning("pointcut()")
    public void doAfterReturning(JoinPoint joinPoint) {
        LOGGER.info("耗时 :{}", ((System.currentTimeMillis() - threadLocal.get())) + "ms");
    }
}

以上可以看出,实现AOP的核心在于切面类

3、总结的一些好用的工具类

操作上可以通过工具类获取某些参数结果

public class AspectUtils {

    public static final String UNKONW = "unkonw";

    /**
     * 获取当前请求对象
     * @return
     */
    public static ServletRequestAttributes getAttributes(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return attributes;
    }
    /**
     * 获取请求示例
     * @return
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes attributes = getAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request;
    }
    /**
     * 获取
     * @param joinPoint
     * @return
     */
    public static Method getMethod(ProceedingJoinPoint joinPoint){
        //获取连接点的方法签名对象
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取方法实例
        Method method = methodSignature.getMethod();
        Class<? extends MethodSignature> aClass = methodSignature.getClass();
        return method;
    }

    /**
     * 通过注解获取注解内部参数内容
     */
    public static  OperationLog  getAnnotationData(ProceedingJoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        OperationLog annotation = method.getAnnotation(OperationLog.class);
        return annotation;
    }

    /**
     * 获取请求参数
     */
    public static void getParameterJson(ProceedingJoinPoint joinPoint){

    }
    /**
     * 获取IP地址
     * @return
     */
    public static String getIPAdress(HttpServletRequest request){
        String ip = request.getHeader("x-forword-for");
        if(ip == null || ip.length() ==0 || UNKONW.equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Clent-IP");
        }
        if(ip == null || ip.length() ==0 || UNKONW.equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Clent-IP");
        }
        if(ip == null || ip.length() ==0 || UNKONW.equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取请求参数
     * @param proceedingJoinPoint
     * @return
     */
    public static  String getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {
        List<Object> argsList = Arrays.stream(proceedingJoinPoint.getArgs())
                .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(argsList)) {
            return "";
        }
        return JSON.toJSONString(argsList.get(0));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值