spring系列之AOP---及5大通知实战

什么是AOP
AOP为Aspect Oriented Programming的缩写
意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

aop应用场景: 日志, 事务等, 抽离共同性质的逻辑,在主业务中某个点切入进去, 但不妨碍业务逻辑的执行

来看一下springAOP中的几个概念

Aspect(切面): Aspect 声明类似于 Java 中的类声明, 表明这个是一个切面,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
Target(目标对象):织入 Advice 的目标对象.。
Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程

在AOP中切面就是与业务逻辑独立,但又垂直存在于业务逻辑的代码结构中的通用功能组合;切面与业务逻辑相交的点就是切点;连接点就是把业务逻辑离散化后的关键节点;切点属于连接点,是连接点的子集;Advice(增强)就是切面在切点上要执行的功能增加的具体操作;在切点上可以把要完成增强操作的目标对象(Target)连接到切面里,这个连接的方式就叫织入。

注解方式声明一个切点

	/**
     * 声明一个切点
     */
    @Pointcut("execution(* com.hc.springaopdemo.controller.*.*(..))")
    private void webLog(){}

Advice 的类型
before advice, 在 join point 前被执行的 advice. 虽然 before advice 是在 join point 前被执行, 但是它并不能够阻止 join point 的执行, 除非发生了异常(即我们在 before advice 代码中, 不能人为地决定是否继续执行 join point 中的代码),对应注解 @Before(value = "webLog()"). webLog()是自己定义的一个切点
after return advice, 在一个 join point 正常返回后执行的 advice, 对应注解@AfterReturning(returning = "o", pointcut = "webLog()")
after throwing advice, 当一个 join point 抛出异常后执行的 advice, 对应注解@AfterThrowing(value = "webLog()", throwing = "exception")
after(final) advice, 无论一个 join point 是正常退出还是发生了异常, 都会被执行的 advice, 对应注解@After(value = "webLog()")
around advice, 在 join point 前和 joint point 退出后都执行的 advice. 这个是最常用的 advice,环绕通知,对应注解@around(value = "webLog()")
introduction,introduction可以为原有的对象增加新的属性和方法。

最强环绕通知用法,盗用一段代码, 最强通知注解,同时搞定前面4个通知

在这里插入图片描述

举一个例子, 每天都要吃饭, 吃饭是主业务, 吃饭有分早中晚3餐,每餐吃的东西不同,甚至有人吃的更多,这些就是主业务
吃饭前洗手,吃饭后洗碗,吃饭时刚好发地震了(异常)报告异常,不管吃没吃完我总知道自己吃了多少, 这些都是早中晚餐都需要做的事情, 不可能将这些事情都在早中晚餐中都写一遍,这样的话显得有点冗余, 这时候aop就发生作用了

实战代码–记录某个方法的调用日志:
这里创建springBoot项目,因为简单 哈哈

创建一个实体类

package com.hc.springaopdemo.entity;

public class User {
    private Integer id;
    private String name;
    private Integer age;

    public User() {
    }

    public User(Integer id ,String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

service就不写了 就简单写个controller业务

package com.hc.springaopdemo.controller;

import com.hc.springaopdemo.annotation.Logs;
import com.hc.springaopdemo.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;

@RestController
@RequestMapping("user")
public class UserController {

    /**
     * 模拟数据库数据
     */
    static HashMap<Integer, User> map = new HashMap<>();
    static{
        User u1 = new User(1,"name1",1);
        User u2 = new User(2,"name2",2);
        User u3 = new User(3,"name3",3);
        User u4 = new User(4,"name4",4);
        User u5 = new User(5,"name5",5);
        map.put(u1.getId(),u1);
        map.put(u2.getId(),u2);
        map.put(u3.getId(),u3);
        map.put(u4.getId(),u4);
        map.put(u5.getId(),u5);
    }

    @Logs
    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public Object findById(Integer id){
        //int i = 1/0;
        User u = map.get(id);
        if(u == null){
            return "用户不存在";
        }
        return u;
    }
}

启动项目, http://localhost:8080/user/getUser?id=1, 其实就可以得到想要的user数据, 如何在不改变主业务代码的情况下记录该方法的调用日志记录

自定义一个注解
只作为一个标记来判断哪个方法需要开启日志记录(毕竟中餐不洗手也能吃饭)

package com.hc.springaopdemo.annotation;

import java.lang.annotation.*;

/**
 * Demo class
 * 方法调用日志记录
 * @author hc
 * @date 2019/10/14
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Logs {
	//这里就不定义什么参数了,只作为一个标记用
}

定义切面类

package com.hc.springaopdemo.aop;

import com.google.gson.Gson;
import com.hc.springaopdemo.annotation.Logs;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
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;

@Aspect//这个注解的作用是:将一个类定义为一个切面类
@Component//这个注解的作用:把切面类加入到IOC容器中
@Order(1)//这个注解的作用是:标记切面类的处理优先级,i值越小,优先级别越高.PS:可以注解类,也能注解到方法上
@Slf4j
public class AspectDemo {

    static Gson gson = new Gson();

    /**
     * 声明一个切点
     */
    @Pointcut("execution(* com.hc.springaopdemo.controller.*.*(..))")
    private void webLog(){}

    /**
     * 请求前通知
     * @Before(value = "webLog()") 这个注解的作用是:在切点webLog()前执行方法,内容为指定的切点
     * JoinPoint 切入点对象, 因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
     */
    @Before(value = "webLog()")
    public void methodBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
        //获得请求的方法
        Method method = joinPointObject.getMethod();

        //如果方法上有Logs这个注解,则记录这个方法的调用日志
        if(hasAnnotationOnMethod(method, Logs.class)){
            //打印请求内容
            log.info("================请求内容===============");
            log.info("请求地址:" + request.getRequestURI().toString());
            log.info("请求方式" + request.getMethod());
            log.info("请求类方法" + joinPoint.getSignature());
            log.info("请求类方法参数" + Arrays.toString(joinPoint.getArgs()));
            log.info("================请求内容================");
        }
    }

    /**
     * 业务正常执行完后通知
     * @AfterReturning(returning = "o", pointcut = "webLog()") 这个注解的作用是:在切入点,return后执行,如果想对某些方法的返回参数进行处理,可以在这操作
     */
    @AfterReturning(returning = "o", pointcut = "webLog()")
    public void methodAfterReturing(JoinPoint joinPoint,Object o) {
        log.info("------------正常返回内容--------------");
        log.info("Response内容:" + gson.toJson(o));
        log.info("------------正常返回内容--------------");

    }

    /**
     * 业务异常时通知
     */
    @AfterThrowing(value = "webLog()", throwing = "exception")
    public void methodAfterException(JoinPoint joinPoint,Exception exception){
        log.info("*************异常返回内容*************");
        log.info("业务异常: "+exception.getMessage());
        log.info("*************异常返回内容*************");
    }

    /**
     * 无论业务是否异常,都通知
     */
    @After(value = "webLog()")
    public void methodAfter(JoinPoint joinPoint){
        log.info("++++++++++++发不发生异常都返回++++++++++++");
        log.info("不管业务有没有发生异常,都执行");
        log.info("++++++++++++发不发生异常都返回++++++++++++");
    }

    /**
     * 判断某方法上是否含有某注解
     * @param method
     * @param annotationClazz
     * @return
     */
    private boolean hasAnnotationOnMethod(Method method, Class annotationClazz){
        //使用反射获取注解信息
        Annotation a = method.getAnnotation(annotationClazz);
        if (a == null){
            return false;
        }
        return true;
    }
}

启动项目访问 , 这里来个正常的,不人为制造异常, 后面的环绕通知人为造个异常
在这里插入图片描述
注意输出顺序

环绕通知按照上面那张图片来改就完了
当使用@Around处理时,我们需要将切点参数定义为ProceedingJoinPoint类型,该类是JoinPoint的子类。
代码

package com.hc.springaopdemo.aop;

import com.google.gson.Gson;
import com.hc.springaopdemo.annotation.Logs;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
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;

@Aspect//这个注解的作用是:将一个类定义为一个切面类
@Component//这个注解的作用:把切面类加入到IOC容器中
@Order(1)//这个注解的作用是:标记切面类的处理优先级,i值越小,优先级别越高.PS:可以注解类,也能注解到方法上
@Slf4j
public class AspectDemo2 {

    static Gson gson = new Gson();

    /**
     * 声明一个切点
     */
    @Pointcut("execution(* com.hc.springaopdemo.controller.*.*(..))")
    private void webLog(){}


    /**
     * 请求前通知
     * @Before(value = "webLog()") 这个注解的作用是:在切点webLog()前执行方法,内容为指定的切点
     * JoinPoint 切入点对象, 因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
     */
    @Around(value = "webLog()")
    public Object methodAround(ProceedingJoinPoint joinPoint) throws Throwable{
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
        //获得请求的方法
        Method method = joinPointObject.getMethod();
        //如果方法上有Logs这个注解,则记录这个方法的调用日志
        if(hasAnnotationOnMethod(method, Logs.class)){
            //打印请求内容
            log.info("==============请求前内容=============");
            log.info("请求地址:" + request.getRequestURI().toString());
            log.info("请求方式" + request.getMethod());
            log.info("请求类方法" + joinPoint.getSignature());
            log.info("请求类方法参数" + Arrays.toString(joinPoint.getArgs()));
            log.info("==============请求前内容==============");
        }

        try {

            log.info("------------正常返回内容--------------");
            log.info("Response内容:" + gson.toJson(joinPoint.proceed()));
            log.info("------------正常返回内容--------------");

        }catch (Exception e){
            log.info("*************异常返回内容*************");
            log.info("业务异常: "+e.getMessage());
            log.info("*************异常返回内容*************");
        }

        log.info("++++++++++++发不发生异常都返回++++++++++++");
        log.info("不管业务有没有发生异常,都执行");
        log.info("++++++++++++发不发生异常都返回++++++++++++");


        return joinPoint.proceed();

    }

    /**
     * 判断某方法上是否含有某注解
     * @param method
     * @param annotationClazz
     * @return
     */
    private boolean hasAnnotationOnMethod(Method method, Class annotationClazz){
        //使用反射获取注解信息
        Annotation a = method.getAnnotation(annotationClazz);
        if (a == null){
            return false;
        }
        return true;
    }

}

启动项目看一下结果, 我这里造了一个异常,看异常后,后面的输出不
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值