首页 > 编程语言 >学习笔记——Spring中的AOP(AspectJ);Spring中AOP概述;Spring中AOP相关术语;AspectJ中切入点表达式;AspectJ中JoinPoint对象;AspectJ中通知

学习笔记——Spring中的AOP(AspectJ);Spring中AOP概述;Spring中AOP相关术语;AspectJ中切入点表达式;AspectJ中JoinPoint对象;AspectJ中通知

时间:2023-01-18 15:11:06浏览次数:49  
标签:joinPoint methodName Spring AOP Object 通知 方法 AspectJ

2023-01-18

一、Spring中的AOP

1、AspectJ

(1)简介

Java社区里最完整最流行的AOP框架

在Spring2.0以上版本中,可以使用AspectJ注解或基于XML配置的AOP

(2)使用AspectJ步骤

①在spring核心包的基础上添加支持jar包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.10</version>
</dependency>

②创建spring的配置文件,配置自动扫描的包和AspectJ注解支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    开启组件扫描-->
    <context:component-scan base-package="com.hh"></context:component-scan>
<!--    开启AspectJ注解支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

③创建一个类作为切面,在类的上面添加注解

@Component:将当前类标识为一个组件

@Aspect:将当前类标识为切面类(非核心业务提取类)

@Component
@Aspect
public class MyLogging {

    /**
     * 方法之前
     */
    public static void beforeMethod(String methodName,Object[] arg){
        System.out.println("==>Calc中"+methodName+"方法(),参数:"+ Arrays.toString(arg));
    }
    /**
     * 方法之后
     */
    public static void afterMethod(String methodName,Object rs){
        System.out.println("==>Calc中"+methodName+"方法(),结果:"+rs);
    }
}

④将日志类中的方法添加“通知注解”

@Before

⑤测试

二、Spring中AOP概述

1、AOP:Aspect-Oriented Programming,面向切面编程(面向对象的一种补充)

优势:解决了代码分散与代码混乱的问题

2、OOP:Object-Oriented Programming,面向对象编程

三、Spring中AOP相关术语

1、横切关注点

非核心业务代码

2、切面(Aspect)

将横切关注点提取到类中,这个类称之为切面类

3、通知(Advice)

将横切关注点提取到类之后,横切关注点更名为通知

4、目标(Target)

目标对象,指的是需要被代理的对象(实现类CalcImpl)

5、代理(Proxy)

代理对象可以理解为中介

6、连接点(Joinpoint)

通知方法需要指定通知位置,这个位置称之为连接点(通知之前)

7、切入点(pointcut)

通知方法需要指定通知位置,这个位置称之为切入点(通知之后)

四、AspectJ中切入点表达式

1、语法:@Before(value="execution(权限修饰符 返回值类型 包名.类名.方法名(参数类型))")

2、通配符

(1)【*】

*:可以代表任意权限修饰符&返回值类型

*:可以代表任意包名、任意类名、任意方法名

(2)【..】

..  代表任意参数类型及参数个数

3、重用切入点表达式

(1)使用@PointCut注解,提取可重用的切入点表达式

 //重用切入点表达式
    @Pointcut("execution(* com.hh.aop.CalcImpl.*(..))")
    public void myPointCut(){}

(2)使用方法名()引入切入点表达式

@Before(value = "myPointCut()")
@After("myPointCut()")

五、AspectJ中JoinPoint对象

1、JoinPoint:切入点对象

2、作用:

(1)获取方法名称

String methodName = joinPoint.getSignature().getName();

注:joinPoint.getSignature()  表示方法签名(方法签名=方法名+参数列表)

(2)获取参数

Object[] args = joinPoint.getArgs();

六、AspectJ中通知

1、前置通知

(1)语法:@Before

(2)执行时机:指定方法之前执行

指定方法:即切入点表达式设置位置

注:如果目标方法中有异常,会执行

(3)示例代码

@Before(value = "myPointCut()")
    public void beforeMethod(JoinPoint joinPoint){
        //获取方法名称
        String methodName = joinPoint.getSignature().getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println("【前置通知】==>Calc中"+methodName+"方法(),参数:"+ Arrays.toString(args));
    }

 

2、后置通知

(1)语法:@After

(2)执行时机:指定方法所有通知执行之后执行

注:如果目标方法中有异常,会执行

(3)示例代码

 @After("myPointCut()")
    public void afterMethod(JoinPoint joinPoint){
        //获取方法名称
        String methodName = joinPoint.getSignature().getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println("【后置通知】==>Calc中"+methodName+"方法(),之后执行:"+ Arrays.toString(args));
    }

 

3、返回通知

(1)语法:@AfterReturnning

(2)执行时机:指定方法返回结果时执行

注:@AfterReturnning中returning属性中的的属性名与入参中参数名一致;

如果目标方法中有异常,不执行

(3)示例代码

@AfterReturning(value = "myPointCut()",returning = "rs")
    public void afterReturnning(JoinPoint joinPoint,Object rs){
        //获取方法名称
        String methodName = joinPoint.getSignature().getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println("【返回通知】==>Calc中"+methodName+"方法(),返回结果执行!结果:"+ rs);
    }

4、异常通知

(1)语法:@AfterThrowing

(2)执行时机::指定方法出现异常时执行

注:@AfterThrowing中throwing属性中的的属性名与入参中参数名一致;

如果目标方法中有异常,执行

(3)示例代码

@AfterThrowing(value = "myPointCut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        //获取方法名称
        String methodName = joinPoint.getSignature().getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println("【异常通知】==>Calc中"+methodName+"方法(),出现异常时执行!异常:"+ ex);
    }

说明:

①有异常:前置通知—>异常通知—>后置通知

②无异常:前置通知—>返回通知—>后置通知

5、环绕通知(前四个通知整合)

(1)语法:@Around

(2)作用:整合前四个通知

(3)注意:参数中必须使用ProceedingJoinPoint

(4)示例代码

 @Around(value = "myPointCut()")
    public Object aroundMethod(ProceedingJoinPoint pjp){

        //获取方法名称
        String methodName = pjp.getSignature().getName();
        //获取参数
        Object[] args = pjp.getArgs();
        //定义返回值
        Object rs = null;
        try {
            //前置通知 )
            System.out.println("【前置通知】==>Calc中"+methodName+"方法(),参数:"+ Arrays.toString(args));
            //触发目标对象的目标方法(加减乘除)
            rs = pjp.proceed();
            //返回通知(有异常不执行
            System.out.println("【返回通知】==>Calc中"+methodName+"方法(),返回结果执行!结果:"+ rs);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            //异常通知
            System.out.println("【异常通知】==>Calc中"+methodName+"方法(),出现异常时执行!异常:"+ throwable);
        }finally {
            //后置通知(有异常执行)
            System.out.println("【后置通知】==>Calc中"+methodName+"方法(),之后执行:"+ Arrays.toString(args));
        }
        return rs;
    }

 

标签:joinPoint,methodName,Spring,AOP,Object,通知,方法,AspectJ
From: https://www.cnblogs.com/isDaHua/p/17059407.html

相关文章

  • 一文搞定SpringCloud Alibaba全部知识点!
    文章目录1.分布式架构简介1.1.分布式架构1.2.常见的微服务架构解决方案1.3.分布式系统核心组件图2.AlibabaCloud架构环境准备2.1.创建maven聚合项目2.2.配置MyBa......
  • 【集成-Jedis】SpringBoot集成Jedis
    将jedis的依赖放进Maven<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.......
  • Springboot:aop注解实现
    概念Aop原指面向切面编程。但在spring中对aop的实现指的是:对方法执行前后加入其它逻辑代码,达到增强方法的目的spring-aop的底层实现一般实现aop,有两种方案:JDK动态......
  • SpringShell使用说明
    一、添加依耐(pom.xml)<dependency><groupId>org.springframework.shell</groupId><artifactId>spring-shell-starter</artifactId><version>2.1.4</version><......
  • SpringBoot2注解:@Configuration
    @configuration@Configuration这个注解作用就是告诉springboot这是一个配置类。配置类以及类里的方法都可以作为bean。用@Bean标记@Configuration包含 proxyBeanMeth......
  • 学习笔记——AOP-代理模式
    2023-01-18一、AOP前奏-代理模式1、手动实现动态代理环境搭建(1)基于接口实现动态代理:JDK动态代理(2)基于继承实现动态代理:Cglib、javassist动态代理2、实现动态代理的步......
  • SpringCloud-入门简介
    https://www.cnblogs.com/xuwc/p/13995814.html参考:https://www.cnblogs.com/lizm166/p/11156311.htmlhttps://www.cnblogs.com/senlinyang/p/8591294.htmlhttps://zhu......
  • 通过Docker启动Solace,并在Spring Boot通过JMS整合Solace
    1简介Solace是一个强大的实时性的事件驱动消息队列。本文将介绍如何在Spring中使用,虽然代码使用的是SpringBoot,但并没有使用相关starter,跟Spring的整合一样,可通用。JMS......
  • 学习笔记——Spring中组件扫描(包含扫描、排除扫描)、Spring中完全注解开发;Spring整合Ju
    2023-01-18一、Spring中组件扫描1、默认使用的情况<context:component-scanbase-package="com.hh"></context:component-scan>2、包含扫描注:使用包含扫描之前,必须......
  • SpringCloud Config分布式配置中心
    1、介绍①what微服务意味着需要将单体拆成很多子服务,每个服务都需要配置才能运行。所以需要一套集中式,动态的配置管理,来解决这个问题。SpringCloudConfig为微服务架构......