ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AOP的基本组成部分

2021-12-16 22:59:42  阅读:191  来源: 互联网

标签:基本 log 通知 joinPoint 切点 切面 组成部分 AOP public


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;
   }

}

标签:基本,log,通知,joinPoint,切点,切面,组成部分,AOP,public
来源: https://blog.csdn.net/ajdhfla/article/details/121985558

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有