AspectJ 是一个基于 Java 语言的 AOP 框架。在 Spring 2.0 以后,新增了对 AspectJ 框架的支持。在 Spring 框架中建议使用 AspectJ 框架开发 AOP。

2.1、 AspectJ 框架 jar 包
aspectjweaver-1.9.5.jar
2.2、 Spring 框架 jar 包
spring-beans-5.2.7.RELEASE.jar
spring-context-5.2.7.RELEASE.jar
spring-core-5.2.7.RELEASE.jar
spring-expression-5.2.7.RELEASE.jar
spring-aop-5.2.7.RELEASE.jar
spring-aspects-5.2.7.RELEASE.jar
commons-logging-1.2.jar
通过 XML 文件配置 AOP
- 通过 AspectJ 配置方式
- 通过 Spring 的 Schema_based 方式
通过注解配置 AOP
在 AspectJ 框架中使用 xml 文件中配置 AOP 的方式:
- AspectJ 配置方式
- Schema-based 配置方式
AspectJ 配置方式是指使用 AspectJ 框架的配置方式来配置切面。在使用 AspectJ 配置切面时,切面不需要实现一些特定的接口。
1.1、 创建切面
1.2、 Execution 表达式
Execution 是 AspectJ 框架中的一种表达式,用于配置切点。
基本语法格式为:
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
其中<修饰符模式>与<异常模式>为可选。
execution(public * com.bjsxt.service….(…))
说明:

- 示例 1:
execution(public * com.bjsxt.service.impl.Student.test())
切点是 com.bjsxt.service.impl 包中 Student 类中所有 public 修饰的无参数的 test方法。 - 示例 2:
execution(* com.bjsxt.service.impl.Student.test(String,int))
切点是 com.bjsxt.service.impl 包中 Student 类中参数类型是 String,int 类型的 test方法。 - 示例 3:
execution(* com.bjsxt.service.impl.Student.test(…))
切点是 com.bjsxt.service.impl 包中 Student 类中任意参数的 test 方法。 - 示例 4
execution(* com.bjsxt.service..(…))
切点是 com.bjsxt.service 包中所有接口中任意参数的所有方法。
1.3、 使用 AspectJ 方式配置切面
1、创建目标对象
2、开启 aop 命名空间
3、配置切面
4、创建测试类
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/6358.html