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集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq