AOP的基本组成部分

2021/12/16 23:12:02

本文主要是介绍AOP的基本组成部分,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、通知(Advise)
* before 目标方法执行前执行,前置通知 
* after 目标方法执行后执行,后置通知 
* after returning 目标方法返回时执行 ,后置返回通知 
* after throwing 目标方法抛出异常时执行 异常通知 
* around 在目标函数执行中执行,可控制目标函数是否执行,环绕通知
2、连接点(JoinPoint)
连接点是在应用执行过程中能够插入切面的一个点。

3、切点(PointCut)
一个切面并不需要通知应用的所有连接点,切点有助于缩小切面所通知的连接点范围。如果说通知定义了切面的“什么”和“何时”的话,那么切点就定义了“何处”。因此,切点其实就是定义了需要执行在哪些连接点上执行通知。

4、切面(Aspect)
切面是通知和切点的结合。通知和切点共同定义了切面的全部内容——它是什么,在何时和在何处完成其功能。

5、引入(Introduction)
引入允许我们向现有的类添加新方法或属性。

6、织入(Weaving)
织入是把切面应用到目标对象并创建新的代理对象的过程。切面在指定的连接点被织入到目标对象中。在目标对象的生命周期中有很多个点可以进行织入。

实现入参出参打印

@Aspect //声明为切面类       
@Component //将切面类交给容器管理@Slf4j
public class DemoAop {

    @Autowired
   private HttpServletResponse response;


    //定义切点,注解为切入点
    @Pointcut("within(com.fourthgroup.schoolmanagementsystem.controller..*)&& !within(com.fourthgroup.schoolmanagementsystem.controller.LoginController)")

    public void viewRecordsPointCut() {

    }



    @Before("viewRecordsPointCut()")
    public void before(JoinPoint joinPoint) throws Throwable {
        log.info("进入Before通知...");
    }

    @After("viewRecordsPointCut()")
    public void after(JoinPoint joinPoint) throws Throwable {
        log.info("进入After通知....");
    }

    @AfterReturning("viewRecordsPointCut()")
    public void afterReturning(JoinPoint joinPoint) throws Throwable {
        log.info("进入AfterReturning通知....");
   }

    @AfterThrowing("viewRecordsPointCut()")
    public void afterThrowing(JoinPoint joinPoint) throws Throwable {
        log.info("进入AfterThrowing通知....");
    }


    @Around("viewRecordsPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("进入controller==>Around通知....");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //打印入参
        log.info("请求入参 {}",new Gson().toJson(joinPoint.getArgs()));

        Object object = joinPoint.proceed();
        //打印出参
        log.info("请求出参 {}",new Gson().toJson(object));
        return object;
   }

}



这篇关于AOP的基本组成部分的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程