aop笔记整理

aop

aop通知类型,有如下几类。通过测试验证以下结论:

  • 前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常);当通知出现异常,连接点之后不会执行。
  • 后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回;当通知出现异常,不影响连接点执行,影响连接点的返回。
  • 异常通知(After throwing advice):在方法抛出异常退出时执行的通知;当通知出现异常,不影响连接点执行,影响连接点的返回。
  • 最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出),当通知出现异常,不影响连接点执行,影响连接点的返回。
  • 环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。

下面是一个自定义注解

package com.example.mybatisplus.mybatisplusdamo.aop;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
 * {@link MyTestInterfaceBefore}
 *
 * @author clc
 * @date 2022/5/23  17:35
 */
@Target(METHOD)
@Retention(RUNTIME)
public @interface MyTestInterfaceBefore
{
}

package com.example.mybatisplus.mybatisplusdamo.aop;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
 * {@link MyTestInterfaceAfter}
 *
 * @author clc
 * @date 2022/5/23  17:35
 */
@Target(METHOD)
@Retention(RUNTIME)
public @interface MyTestInterfaceAfter
{
}

package com.example.mybatisplus.mybatisplusdamo.aop;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
 * {@link MyTestInterfaceAfterRetrun}
 *
 * @author clc
 * @date 2022/5/23  17:35
 */
@Target(METHOD)
@Retention(RUNTIME)
public @interface MyTestInterfaceAfterRetrun
{
}

package com.example.mybatisplus.mybatisplusdamo.aop;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
 * {@link MyTestInterfaceAfterThrow}
 *
 * @author clc
 * @date 2022/5/23  17:35
 */
@Target(METHOD)
@Retention(RUNTIME)
public @interface MyTestInterfaceAfterThrow
{
}

package com.example.mybatisplus.mybatisplusdamo.aop;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
 * {@link MyTestInterface}
 *
 * @author clc
 * @date 2022/5/23  17:35
 */
@Target(METHOD)
@Retention(RUNTIME)
public @interface MyTestInterface
{
}

下面是一个切面类

package com.example.mybatisplus.mybatisplusdamo.aop;

import com.example.mybatisplus.mybatisplusdamo.sql.p3.SqlMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
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.RequestMapping;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 * <p>切面类</p>
 * 通知类型
 * 前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常);当通知出现异常,连接点之后不会执行。
 * 后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回;当通知出现异常,不影响连接点执行,影响连接点的返回。
 * 异常通知(After throwing advice):在方法抛出异常退出时执行的通知;当通知出现异常,不影响连接点执行,影响连接点的返回。
 * 最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出),当通知出现异常,不影响连接点执行,影响连接点的返回。
 * 环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。
 */
@Component
@Aspect
public class MyTestInterfaceAspect
{
    private static Logger logger = LoggerFactory.getLogger(MyTestInterfaceAspect.class);

    @Autowired
    private SqlMapper sqlMapper;

    @Pointcut("@annotation(com.example.mybatisplus.mybatisplusdamo.aop.MyTestInterface01)")
    public void pointCut()
    {

    }

    @Before("pointCut()")
    public void before()
    {
        logger.info("pointCut()" + "-->" + "before");
    }

    @Before("@annotation(com.example.mybatisplus.mybatisplusdamo.aop.MyTestInterfaceBefore)")
    public Object handleLogResult(JoinPoint pjp)
        throws Throwable
    {
        logger.info("================================aop/MyTestInterfaceBefore,测试完成");
        Signature sig = pjp.getSignature();
        MethodSignature msig = (MethodSignature)sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(),
            msig.getParameterTypes());
        RequestMapping annotation = currentMethod.getAnnotation(RequestMapping.class);

        if (annotation == null)
        {
            return pjp;
        }
        else
        {
            logger.error("annotation: {}", annotation);
            if (new Random().nextBoolean())
            {
                //NPE
                HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
                objectObjectHashMap.get("dd").toString();
            }
            Map<String, Object> user = new HashMap<>();
            user.put("id", new Random().nextInt(100000) + 101);
            user.put("name", "Before-chenlc");
            user.put("age", 100);
            sqlMapper.add(user);
        }
        return pjp;
    }

    @After("@annotation(com.example.mybatisplus.mybatisplusdamo.aop.MyTestInterfaceAfter)")
    public Object handleLogResult1(JoinPoint pjp)
        throws Throwable
    {
        logger.info("================================aop/MyTestInterfaceAfter,测试完成");
        Signature sig = pjp.getSignature();
        MethodSignature msig = (MethodSignature)sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(),
            msig.getParameterTypes());
        RequestMapping annotation = currentMethod.getAnnotation(RequestMapping.class);
        if (new Random().nextBoolean())
        {
            //NPE
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.get("dd").toString();
        }
        if (annotation == null)
        {
            return pjp;
        }
        else
        {
            logger.error("annotation: {}", annotation);
            Map<String, Object> user = new HashMap<>();
            user.put("id", new Random().nextInt(100000) + 101);
            user.put("name", "After-chenlc");
            user.put("age", 100);
            sqlMapper.add(user);
        }
        return pjp;
    }

    @AfterReturning("@annotation(com.example.mybatisplus.mybatisplusdamo.aop.MyTestInterfaceAfterRetrun)")
    public Object handleLogResult2(JoinPoint pjp)
        throws Throwable
    {
        logger.info("================================aop/MyTestInterfaceAfterReturning,测试完成");
        Signature sig = pjp.getSignature();
        MethodSignature msig = (MethodSignature)sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(),
            msig.getParameterTypes());
        RequestMapping annotation = currentMethod.getAnnotation(RequestMapping.class);
        if (new Random().nextBoolean())
        {
            //NPE
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.get("dd").toString();
        }
        if (annotation == null)
        {
            return pjp;
        }
        else
        {
            logger.error("annotation: {}", annotation);
            Map<String, Object> user = new HashMap<>();
            user.put("id", new Random().nextInt(100000) + 101);
            user.put("name", "AfterReturning-chenlc");
            user.put("age", 100);
            sqlMapper.add(user);
        }
        return pjp;
    }

    @AfterThrowing("@annotation(com.example.mybatisplus.mybatisplusdamo.aop.MyTestInterfaceAfterThrow)")
    public Object handleLogResult3(JoinPoint pjp)
        throws Throwable
    {
        logger.info("================================aop/MyTestInterfaceAfterThrow,测试完成");
        Signature sig = pjp.getSignature();
        MethodSignature msig = (MethodSignature)sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(),
            msig.getParameterTypes());
        RequestMapping annotation = currentMethod.getAnnotation(RequestMapping.class);
        if (new Random().nextBoolean())
        {
            //NPE
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.get("dd").toString();
        }
        if (annotation == null)
        {
            return pjp;
        }
        else
        {
            logger.error("annotation: {}", annotation);
            Map<String, Object> user = new HashMap<>();
            user.put("id", new Random().nextInt(100000) + 101);
            user.put("name", "AfterThrowing-chenlc");
            user.put("age", 100);
            sqlMapper.add(user);
        }
        return pjp;
    }

    @Around("@annotation(com.example.mybatisplus.mybatisplusdamo.aop.MyTestInterface)")
    public Object handleLogResult4(ProceedingJoinPoint pjp)
        throws Throwable
    {
        logger.info("================================aop/MyTestInterfaceAround,测试完成");
        Signature sig = pjp.getSignature();
        MethodSignature msig = (MethodSignature)sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(),
            msig.getParameterTypes());
        RequestMapping annotation = currentMethod.getAnnotation(RequestMapping.class);
        if (new Random().nextBoolean())
        {
            //NPE
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.get("dd").toString();
        }
        if (annotation == null)
        {
            return pjp.proceed();
        }
        else
        {
            logger.error("annotation: {}", annotation);
            Map<String, Object> user = new HashMap<>();
            user.put("id", new Random().nextInt(100000) + 101);
            user.put("name", "Around-chenlc");
            user.put("age", 100);
            sqlMapper.add(user);
        }
        return pjp.proceed();
    }
}

下面是测试接口,分别对几个aop通知模型做了测试

package com.example.mybatisplus.mybatisplusdamo.sql.p1;

import com.alibaba.fastjson.JSON;
import com.example.mybatisplus.mybatisplusdamo.aop.*;
import com.example.mybatisplus.mybatisplusdamo.sql.p2.SqlService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;


@Slf4j
@RestController
@ApiOperation(notes = "clc-测试controller", value = "测试-SqlController")
public class SqlController
{
    @Autowired
    private SqlService sqlService;
    
    @MyTestInterfaceBefore
    @RequestMapping(path = "/test/aop/MyTestInterfaceBefore", method = RequestMethod.GET)
    public String testBefore()
        throws Exception
    {
        List<Map<String, Object>> maps = sqlService.allOrgs();
        log.error("map:{}", maps);
        if (new Random().nextBoolean())
        {
            log.info("===========出现异常=====================aop/MyTestInterfaceBefore,测试完成");
            throw new Exception();
        }
        log.info("================================aop/MyTestInterfaceBefore,测试完成");
        return "aop/MyTestInterfaceBefore,测试完成";
    }

    @MyTestInterfaceAfterRetrun
    @RequestMapping(path = "/test/aop/MyTestInterfaceAfterRetrun", method = RequestMethod.GET)
    public String testAfterRetrun()
        throws Exception
    {
        List<Map<String, Object>> maps = sqlService.allOrgs();
        log.error("map:{}", maps);
        if (new Random().nextBoolean())
        {
            log.info("===========出现异常=====================aop/MyTestInterfaceAfterRetrun,测试完成");
            throw new Exception();
        }
        log.info("================================aop/MyTestInterfaceAfterRetrun,测试完成");
        return "aop/MyTestInterfaceAfterRetrun,测试完成";
    }

    @MyTestInterfaceAfterThrow
    @RequestMapping(path = "/test/aop/MyTestInterfaceAfterThrow", method = RequestMethod.GET)
    public String testAfterThrow()
        throws Exception
    {
        List<Map<String, Object>> maps = sqlService.allOrgs();
        log.error("map:{}", maps);
        if (new Random().nextBoolean())
        {
            log.info("===========出现异常=====================aop/MyTestInterfaceAfterThrow,测试完成");
            throw new Exception();
        }
        log.info("================================aop/MyTestInterfaceAfterThrow,测试完成");
        return "aop/MyTestInterfaceAfterThrow,测试完成";
    }

    @MyTestInterfaceAfter
    @RequestMapping(path = "/test/aop/MyTestInterfaceAfter", method = RequestMethod.GET)
    public String testAfter()
        throws Exception
    {
        List<Map<String, Object>> maps = sqlService.allOrgs();
        log.error("map:{}", maps);
        if (new Random().nextBoolean())
        {
            log.info("===========出现异常=====================aop/MyTestInterfaceAfter,测试完成");
            throw new Exception();
        }
        log.info("================================aop/MyTestInterfaceAfter,测试完成");
        return "aop/MyTestInterfaceAfter,测试完成";
    }

    @MyTestInterface
    @RequestMapping(path = "/test/aop/MyTestInterface", method = RequestMethod.GET)
    public String testAround()
        throws Exception
    {
        List<Map<String, Object>> maps = sqlService.allOrgs();
        log.error("map:{}", maps);
        if (new Random().nextBoolean())
        {
            log.info("===========出现异常=====================aop/MyTestInterfaceAround,测试完成");
            throw new Exception();
        }
        log.info("================================aop/interface,测试完成");
        return "aop/interface,测试完成";
    }
    @MyTestInterface01
    @RequestMapping(path = "/test/aop/MyTestInterface01", method = RequestMethod.GET)
    public String testPointcut()
        throws Exception
    {
        List<Map<String, Object>> maps = sqlService.allOrgs();
        log.error("map:{}", maps);
        if (new Random().nextBoolean())
        {
            log.info("===========出现异常=====================aop/MyTestInterfaceAround,测试完成");
            throw new Exception();
        }
        log.info("================================aop/interface,测试完成");
        return "aop/interface,测试完成";
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Spring AOP笔记和源码讲解,我可以给你简单介绍一下。 Spring AOPSpring框架中的一个重要模块,提供了基于代理的AOP实现。AOP(Aspect Oriented Programming)面向切面编程,是一种编程思想,它通过将横切关注点与业务逻辑分离,使得系统的关注点更加清晰,代码更加简洁易懂,同时也提高了代码的可维护性和可扩展性。 在Spring AOP中,切面(Aspect)是一个类,它包含了通知(Advice)和切点(Pointcut)。通知是在切点上执行的操作,例如在方法执行前或执行后执行一些额外的逻辑。而切点则是一个表达式,用于匹配目标对象中的方法,从而确定哪些方法会被通知所影响。 Spring AOP提供了四种通知类型,分别是: 1. 前置通知(Before advice):在目标方法执行之前执行。 2. 后置通知(After returning advice):在目标方法执行之后执行,在目标方法没有抛出异常的情况下。 3. 异常通知(After throwing advice):在目标方法抛出异常后执行。 4. 最终通知(After advice):无论目标方法是否抛出异常,最终通知都会执行。 除了通知之外,Spring AOP还提供了环绕通知(Around advice),它可以在目标方法执行前和执行后执行一些额外的逻辑,并且可以控制目标方法的执行。 在Spring AOP中,代理是通过JDK动态代理或者CGLIB字节码生成技术生成的。如果目标对象实现了接口,则使用JDK动态代理实现代理;如果目标对象没有实现接口,则使用CGLIB字节码生成技术实现代理。 在Spring AOP中,通知和切点都可以使用注解的方式来声明。例如,使用@Aspect注解声明一个切面类,使用@Before、@After、@AfterReturning、@AfterThrowing和@Around注解声明通知方法,使用@Pointcut注解声明切点表达式。 关于Spring AOP源码讲解,它的实现主要涉及到以下几个类: 1. AdvisedSupport类:封装了目标对象、切面和通知等信息。 2. ProxyFactory类:用于生成代理对象的工厂类。 3. AopProxy接口:代理对象的接口。 4. JdkDynamicAopProxy和CglibAopProxy类:实现了AopProxy接口,分别用于基于JDK动态代理和CGLIB字节码生成技术的代理对象。 以上是Spring AOP笔记和简单源码讲解,希望能对你有所帮助。如果有什么不清楚的地方,可以继续问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值