AOP入门

作者: yuaixing003 | 来源:发表于2023-11-30 16:07 被阅读0次

    什么是AOP?

    AOP英文全称:Aspect Oriented Programming(面向切面编程、面向方面编程),其实说白了,面向切面编程就是面向特定方法编程。AOP面向方法编程,就可以做到在不改动这些原始方法的基础上,针对特定的方法进行功能的增强。AOP的作用:在程序运行期间在不修改源代码的基础上对已有方法进行增强(无侵入性: 解耦)

    中间运行的原始业务方法,可能是其中的一个业务方法,比如:我们只想通过 部门管理的 list 方法的执行耗时,那就只有这一个方法是原始业务方法。 而如果,我们是先想统计所有部门管理的业务方法执行耗时,那此时,所有的部门管理的业务方法都是 原始业务方法。 那面向这样的指定的一个或多个方法进行编程,我们就称之为 面向切面编程。

    AOP的优势:减少重复代码,提高开发效率,维护方便

    AOP快速入门

    需求:统计各个业务层方法执行耗时。

    实现步骤:

    1.导入依赖:在pom.xml中导入AOP的依赖

    2.编写AOP程序:针对于特定方法根据业务需要进行编程

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>

    AOP程序:TimeAspect

    @Component

    @Aspect //当前类为切面类

    @Slf4j

    public class TimeAspect {

        @Around("execution(* com.itheima.service.*.*(..))")

        public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {

            //记录方法执行开始时间

            long begin = System.currentTimeMillis();

            //执行原始方法

            Object result = pjp.proceed();

            //记录方法执行结束时间

            long end = System.currentTimeMillis();

            //计算方法执行耗时

            log.info(pjp.getSignature()+"执行耗时: {}毫秒",end-begin);

            return result;

        }

    }

    总结:其实AOP常见的应用场景如下:

    1.记录系统的操作日志

    2.权限控制

    3.事务管理:我们前面所讲解的Spring事务管理,底层其实也是通过AOP来实现的,只要添加@Transactional注解之后,AOP程序自动会在原始方法运行前先来开启事务,在原始方法运行完毕之后提交或回滚事务

    AOP进阶

    通知类型,通知顺序,切入点表达式,连接点

    @Around:环绕通知,此注解标注的通知方法在目标方法前、后都被执行

    @Before:前置通知,此注解标注的通知方法在目标方法前被执行

    @After :后置通知,此注解标注的通知方法在目标方法后被执行,无论是否有异常都会执行

    @AfterReturning : 返回后通知,此注解标注的通知方法在目标方法后被执行,有异常不会执行

    @AfterThrowing : 异常后通知,此注解标注的通知方法发生异常后执行

    @Around环绕通知需要自己调用 ProceedingJoinPoint.proceed() 来让原始方法执行,其他通知不需要考虑目标方法执行

    @Around环绕通知方法的返回值,必须指定为Object,来接收原始方法的返回值,否则原始方法执行完毕,是获取不到返回值的。

    Spring提供了@PointCut注解,该注解的作用是将公共的切入点表达式抽取出来,需要用到时引用该切入点表达式即可。

    @Slf4j

    @Component

    @Aspect

    public class MyAspect1 {

        //切入点方法(公共的切入点表达式)

        @Pointcut("execution(* com.itheima.service.*.*(..))")

        private void pt(){

        }

        //前置通知(引用切入点)

        @Before("pt()")

        public void before(JoinPoint joinPoint){

            log.info("before ...");

        }

        //环绕通知

        @Around("pt()")

        public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

            log.info("around before ...");

            //调用目标对象的原始方法执行

            Object result = proceedingJoinPoint.proceed();

            //原始方法在执行时:发生异常

            //后续代码不在执行

            log.info("around after ...");

            return result;

        }

        //后置通知

        @After("pt()")

        public void after(JoinPoint joinPoint){

            log.info("after ...");

        }

        //返回后通知(程序在正常执行的情况下,会执行的后置通知)

        @AfterReturning("pt()")

        public void afterReturning(JoinPoint joinPoint){

            log.info("afterReturning ...");

        }

        //异常通知(程序在出现异常的情况下,执行的后置通知)

        @AfterThrowing("pt()")

        public void afterThrowing(JoinPoint joinPoint){

            log.info("afterThrowing ...");

        }

    }

    execution主要根据方法的返回值、包名、类名、方法名、方法参数等信息来匹配,语法为:

    execution(访问修饰符? 返回值 包名.类名.?方法名(方法参数) throws 异常?)

    1.其中带?的表示可以省略的部分

    2.访问修饰符:可省略(比如: public、protected)

    3.包名.类名: 可省略

    4.throws 异常:可省略(注意是方法上声明抛出的异常,不是实际抛出的异常)

    示例:@Before("execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))")

    如果只想给查询的方法加上,那方法取名的时候最好遵循一个规范,比如查询的方法都是 get...,

    @Before("execution(* com.itheima.service.impl.DeptServiceImpl.get*(java.lang.Integer))")

    切入点表达式示例

    省略方法的修饰符号

    execution(voidcom.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))

    使用*代替返回值类型

    execution(*com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))

    使用*代替包名(一层包使用一个*)

    execution(*com.itheima.*.*.DeptServiceImpl.delete(java.lang.Integer))

    使用..省略包名

    execution(*com..DeptServiceImpl.delete(java.lang.Integer))

    使用*代替类名

    execution(*com..*.delete(java.lang.Integer))

    使用*代替方法名

    execution(*com..*.*(java.lang.Integer))

    使用 * 代替参数

    execution(*com.itheima.service.impl.DeptServiceImpl.delete(*))

    使用..省略参数

    execution(*com..*.*(..))

    相关文章

      网友评论

          本文标题:AOP入门

          本文链接:https://www.haomeiwen.com/subject/hmxdwdtx.html