Spring AOP 后篇(三): AOP切面编程

2024-02-01 19:48

本文主要是介绍Spring AOP 后篇(三): AOP切面编程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring AOP 后篇: AOP切面编程

该文章参考多篇文章的基础上进行了简化并做少许修改,方便理解。原文章地址如下:

  1. Spring3:AOP
  2. Spring的AOP面向切面编程

一、理解切入点表达式(execution())(重点)

  • 切入点指示符

    • execution:用于匹配方法执行连接点。这是使用Spring AOP时使用的主要切入点指示符。
    • within:限制匹配某些类型中的连接点(使用Spring AOP时在匹配类型中声明的方法的执行)。
    • this:限制与连接点的匹配(使用Spring AOP时执行方法),其中bean引用(Spring AOP代理)是给定类型的实例。
    • target:限制匹配连接点(使用Spring AOP时执行方法),其中目标对象(被代理的应用程序对象)是给定类型的实例。
    • args:限制与连接点的匹配(使用Spring AOP时执行方法),其中参数是给定类型的实例。
    • @target:限制与连接点的匹配(使用Spring AOP时执行方法),其中执行对象的类具有给定类型的注释。
    • @args:限制与连接点的匹配(使用Spring AOP时执行方法),其中传递的实际参数的运行时类型具有给定类型的注释。
    • @within:限制匹配到具有给定注释的类型中的连接点(使用Spring AOP时执行在具有给定注释的类型中声明的方法)。
    • @annotation:限制连接点的匹配,其中连接点的主题(在Spring AOP中执行的方法)具有给定的注释。

    within、this、args等详细解释。。。(todo)

  • execution表达式 详解 (参考Spring AOP 官方文档)

    <!-- * 号表示通配符; 
    如下 execution表达式表示 匹配service包下的所有类的所有带参及不带参数的方法 -->
    <aop:pointcut id="businessServcie" expression="execution(* com.xyz.myapp.service.*.*(..))" />
    
    1. execution() : 表达式主体
    2. 第一个’*'号位置:表示 返回类型;*作用是匹配任何返回类型;
    3. com.xyz.myapp.service: 表示包名,即service包
    4. 第二个’*'号位置:表示 类名;*作用是匹配该包下的所有类
    5. 第三个‘*’号位置:表示 方法名; *作用是匹配所有方法
    6. (…): 表示 括号里面代表参数;()表示匹配一个不带参数的方法;(…)表示匹配任何数量(零个或多个)参数;(*)表示匹配一个采用任何类型参数的方法

切入点其他表达式

  • 常用切入点表达式(execution());Spring AOP官方文档

    1. 执行任何公共方法:
    execution(public * *(..))
    
    1. 执行名称以以下开头的任何方法set
    execution(* set*(..))
    
    1. 执行AccountService接口定义的任何方法:
    execution(* com.xyz.service.AccountService.*(..))
    
    1. 执行service包中定义的任何方法:
    execution(* com.xyz.service.*.*(..))
    
    1. 执行服务包或其子包中定义的任何方法:
    execution(* com.xyz.service..*.*(..))
    
    1. 服务包中的任何连接点(仅在Spring AOP中执行方法):
    within(com.xyz.service.*)
    
    1. 服务包或其子包中的任何连接点(仅在Spring AOP中执行方法):
    within(com.xyz.service..*)
    
    1. 代理实现AccountService接口的任何连接点(仅在Spring AOP中执行方法) :
    this(com.xyz.service.AccountService)
    

二、XML手动配置方式

  1. aopBean.xml
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd"><!-- dao 实例 --><bean id="userDao" class="com.aop.UserDao" /><bean id="orderDao" class="com.aop.OrderDao" /><!-- 切面类:在调用dao类时,先后调用 时间打印及日志打印类 --><bean id="timePrint" class="com.aop.TimePrint" /><bean id="logPrint" class="com.aop.LogPrint" /><aop:config><!-- 定义切面类ref: 指定切面类; order(int):指定切面的优先级来控制通知的执行顺序; order1 》order2--><aop:aspect id="time" ref="timePrint" order="1"><!-- 切入点expression: 定义切入点表达式;即定义要切入的类或方法--><aop:pointcut id="addTime" expression="execution(* com.aop.userDao.*(..))" /><!-- 前置通知: 在目标方法调用前执行 method:切面类执行的方法;即目标类调用前执行的方法--><aop:before method="printTime" pointcut-ref="addTime" /><!-- 后置通知: 在目标方法调用后执行 --><aop:after method="printTime" pointcut-ref="addTime" /></aop:aspect><aop:aspect id="log" ref="logPrint" order="2"><aop:pointcut id="printLog" expression="execution(* com.aop.*.*(..))" /><aop:before method="LogBefore" pointcut-ref="printLog" /><aop:after method="LogAfter" pointcut-ref="printLog" /></aop:aspect></aop:config>
</beans>
  1. aopBean.xml

    <?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- dao 实例   --><bean id="userDao" class="com.aop.dao.UserDao"></bean><bean id="orderDao" class="com.aop.dao.OrderDao"></bean><!-- 切面类 --><bean id="aop" class="com.aop.dao.Aop"></bean><!-- Aop配置 --><aop:config><!-- 定义一个切入点表达式: 拦截哪些方法 --><aop:pointcut  id="pt" expression="execution(* com.aop.dao.*.*(..))"/><!-- 切面 --><aop:aspect ref="aop"><!-- 环绕通知 --><aop:around method="around" pointcut-ref="pt"/><!-- 前置通知: 在目标方法调用前执行 --><aop:before method="begin" pointcut-ref="pt"/><!-- 后置通知: --><aop:after method="after" pointcut-ref="pt"/><!-- 返回后通知 --><aop:after-returning method="afterReturning" pointcut-ref="pt"/><!-- 异常通知 --><aop:after-throwing method="afterThrowing" pointcut-ref="pt"/></aop:aspect></aop:config>
    </beans>
    

三、@Aspectj 注解方式

  1. bean.xml 配置aop注解扫描

    <?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 使用注解时要开启注解扫描 要扫描的包 --><context:component-scan base-package="cn.itcast.e_aop_anno"></context:component-scan><!-- 开启aop注解方式 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>
    
    1. 使用切面类使用@Aspect 注解

      @Component  //加入IOC容器
      @Aspect  // 指定当前类为切面类
      public class Aop {// 指定切入点表达式: 拦截哪些方法; 即为哪些类生成代理对象//解释@Pointcut("execution(* cn.itcast.e_aop_anno.*.*(..))")//@Pointcut("execution(*    切入点表达式固定写法, cn.itcast.e_aop_anno表示包.类名(可以用*表示包下所有的类).方法名(可以用*表示类下所有的方法)(..)表示参数可以用..@Pointcut("execution(* cn.itcast.e_aop_anno.*.*(..))")public void pointCut_(){}//@Before("execution(* cn.itcast.e_aop_anno.*.*(..))")每个方法需要写相同的引用,所以将相同的部分抽取到一个空的方法中pointCut_(),// 前置通知 : 在执行目标方法之前执行@Before("pointCut_()")public void begin(){System.out.println("开始事务/异常");}// 后置/最终通知:在执行目标方法之后执行  【无论是否出现异常最终都会执行】@After("pointCut_()")public void after(){System.out.println("提交事务/关闭");}// 返回后通知: 在调用目标方法结束后执行 【出现异常不执行】@AfterReturning("pointCut_()")public void afterReturning() {System.out.println("afterReturning()");}// 异常通知: 当目标方法执行异常时候执行此关注点代码@AfterThrowing("pointCut_()")public void afterThrowing(){System.out.println("afterThrowing()");}// 环绕通知:环绕目标方式执行@Around("pointCut_()")public void around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("环绕前....");pjp.proceed();  // 执行目标方法System.out.println("环绕后....");}}
      

这篇关于Spring AOP 后篇(三): AOP切面编程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/668287

相关文章

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件