Java之Spring AOP入门到精通【IDEA版】(一篇文章精通系列)

x33g5p2x  于2021-10-17 转载在 Java  
字(25.7k)|赞(0)|评价(0)|浏览(1380)

一、设计模式-代理模式

代理模式:给某一个对象提供一个代理对象,并由代理对象控制对源对象的引用。代理
就是一个人或一个机构代表另一个人或者一个机构采取行动。某些情况下,客户不想或者不
能够直接引用一个对象,代理对象可以在客户和目标对象直接起到中介的作用。

客户端分辨不出代理主题对象与真实主题对象。代理模式可以并不知道真正的被代理对象,而仅仅持有一个被代理对象的接口,这时候代理对象不能够创建被代理对象,被代理对象必须有系统的
其他角色代为创建并传入。

为什么要使用代理模式呢?
第一,它有间接的特点,可以起到中介隔离作用。
就好比在租房的时候,房东可能不在本地,而短期内又不能赶回来,此时中介的出场,就作为房东的代理实现和我们签订承租合同。而我们和房东之间就没有耦合了。

第二,它有增强的功能。还以租房为例,我们首先考虑的是找一个靠谱的中介,由中介
给我们提供房源信息,并且告诉我们房屋的细节,合同的细节等等。
当然我们也可以自己去
找一些个人出租的房屋,但是在这之中,我们要了解租赁合同,房屋验收,租金监管等情
况,这无疑对我们是繁重的工作。
而有了中介作为我们的代理中间人,他把了解到的信息告诉我们,我们一样可以租到房子,而不必做那些繁重的工作。

二、AOP思想及实现原理

1、AOP思想

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方
式和运行期动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

2、实现原理

在上面的概念中描述出aop的实现原理是基于动态代理技术实现的。下面是针对动态代
理的一些介绍:
特点: 字节码随用随创建,随用随加载
分类: 基于接口的动态代理,基于子类的动态代理
作用: 不修改源码的基础上对方法增强

(1)基于接口的动态代理:

提供者是:JDK官方
使用要求:被代理类最少实现一个接口。
涉及的类:Proxy
创建代理对象的方法:newProxyInstance

方法的参数:
ClassLoader:类加载器。用于加载代理对象的字节码的。和被代理对象使用相同的类加载器。固定写法。
Class[]:字节码数组。用于给代理对象提供方法。和被代理对象具有相同的方法。

被代理类是一个普通类:被代理类对象.getClass().getInterfaces();

被代理类是一个接口:new Class[]{被代理了.class}它也是固定写法InvocationHanlder:要增强的方法。此处是一个接口,我们需要提供它的实现类。通常写的是匿名内部类。增强的代码谁用谁写。

基于子类的动态代理

提供者是:第三方cglib包,在使用时需要先导包(maven工程导入坐标即可)

使用要求:被代理类不能是最终类,不能被final修饰

涉及的类:Enhancer

创建代理对象的方法:create

方法的参数:

Class:字节码。被代理对象的字节码。可以创建被代理对象的子类,还可以获取被代理对象的类加载器。
Callback:增强的代码。谁用谁写。通常都是写一个接口的实现类或者匿名内部类。

Callback中没有任何方法,所以我们一般使用它的子接口:MethodInterceptor

3、Spring中AOP的术语

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,指的是方法,因为spring只支持方法类型的连接点。

Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义。

Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。

Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, 可以在运行期为类动态地添加一些方法或Field。

Target(目标对象):代理的目标对象。

Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。

Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类。

Aspect(切面):是切入点和通知(引介)的结合。

三、Spring注解驱动AOP开发入门

1、写在最前

a.Spring的aop是基于ioc的。所以需要有spring的ioc基础。(本篇内容不对ioc进行讲解)
b.本章节我们只是对aop的使用做基本功能展示,目的是为了以此讲解aop中的注解和执行原理分析。

2、注解驱动入门案例介绍

需求:实现在执行service方法时输出执行日志。(除了业务层外,表现层和持久层也可以实现)

3、案例实现

  • 工程搭建

  • 引入依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.itbluebox</groupId>
  5. <artifactId>spring-aop</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>5.1.6.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-jdbc</artifactId>
  16. <version>5.1.6.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <version>5.1.45</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-test</artifactId>
  26. <version>5.1.6.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>junit</groupId>
  30. <artifactId>junit</artifactId>
  31. <version>4.12</version>
  32. </dependency>
  33. <!--导入yaml文件解析器坐标-->
  34. <dependency>
  35. <groupId>org.yaml</groupId>
  36. <artifactId>snakeyaml</artifactId>
  37. <version>1.23</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.aspectj</groupId>
  41. <artifactId>aspectjweaver</artifactId>
  42. <version>1.8.13</version>
  43. </dependency>
  44. </dependencies>
  45. </project>
  • 创建User实体类

  1. package cn.itbluebox.pojo;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class User implements Serializable {
  5. private String id;
  6. private String username;
  7. private String password;
  8. private String email;
  9. private Date birthday;
  10. private String gender;
  11. private String mobile;
  12. private String nickname;
  13. public User() {
  14. }
  15. public User(String id, String username, String password, String email, Date birthday, String gender, String mobile, String nickname) {
  16. this.id = id;
  17. this.username = username;
  18. this.password = password;
  19. this.email = email;
  20. this.birthday = birthday;
  21. this.gender = gender;
  22. this.mobile = mobile;
  23. this.nickname = nickname;
  24. }
  25. public String getId() {
  26. return id;
  27. }
  28. public void setId(String id) {
  29. this.id = id;
  30. }
  31. public String getUsername() {
  32. return username;
  33. }
  34. public void setUsername(String username) {
  35. this.username = username;
  36. }
  37. public String getPassword() {
  38. return password;
  39. }
  40. public void setPassword(String password) {
  41. this.password = password;
  42. }
  43. public String getEmail() {
  44. return email;
  45. }
  46. public void setEmail(String email) {
  47. this.email = email;
  48. }
  49. public Date getBirthday() {
  50. return birthday;
  51. }
  52. public void setBirthday(Date birthday) {
  53. this.birthday = birthday;
  54. }
  55. public String getGender() {
  56. return gender;
  57. }
  58. public void setGender(String gender) {
  59. this.gender = gender;
  60. }
  61. public String getMobile() {
  62. return mobile;
  63. }
  64. public void setMobile(String mobile) {
  65. this.mobile = mobile;
  66. }
  67. public String getNickname() {
  68. return nickname;
  69. }
  70. public void setNickname(String nickname) {
  71. this.nickname = nickname;
  72. }
  73. @Override
  74. public String toString() {
  75. return "User{" +
  76. "id='" + id + '\'' +
  77. ", username='" + username + '\'' +
  78. ", password='" + password + '\'' +
  79. ", email='" + email + '\'' +
  80. ", birthday=" + birthday +
  81. ", gender='" + gender + '\'' +
  82. ", mobile='" + mobile + '\'' +
  83. ", nickname='" + nickname + '\'' +
  84. '}';
  85. }
  86. }
  • 业务层接口

  1. package cn.itbluebox.service;
  2. import cn.itbluebox.pojo.User;
  3. public interface UserService {
  4. /* 保存用户 */
  5. void save(User user);
  6. }
  • 业务层接口实现类

  1. package cn.itbluebox.service.impl;
  2. import cn.itbluebox.pojo.User;
  3. import cn.itbluebox.service.UserService;
  4. import org.springframework.stereotype.Service;
  5. @Service("userService")
  6. public class UserServiceImpl implements UserService {
  7. public void save(User user) {
  8. System.out.println("保存用户信息:"+user);
  9. }
  10. }
  • 日志工具类

  1. package cn.itbluebox.utils;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.*;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @Aspect
  7. public class LogUtil {
  8. /* Pointcut通用切入点表达式 execution允许 */
  9. @Pointcut("execution(* cn.itbluebox.service.impl.*.*(..))")
  10. private void pt1(){}
  11. /* 前置通知 */
  12. @Before("pt1()")
  13. public void beforeLog(){
  14. System.out.println("执行切入点方法前记录日志");
  15. }
  16. /* 后置通知 */
  17. @AfterReturning("pt1()")
  18. public void afterReturningLog(){
  19. System.out.println("正常执行切入点方法后记录日志");
  20. }
  21. /* 异常通知 */
  22. @AfterReturning("pt1()")
  23. public void afterThrowingLog(){
  24. System.out.println("执行切入点方法产生异常后记录日志");
  25. }
  26. /* 最终通知 */
  27. @After("pt1()")
  28. public void afterLog(){
  29. System.out.println("无论切入点方法执行是否异常都记录日志");
  30. }
  31. /* 环绕通知 */
  32. @Around("pt1()")
  33. public Object aroundPrintLog(ProceedingJoinPoint pjp){
  34. //1.定义返回值
  35. Object rtValue = null;
  36. try{
  37. //前置通知
  38. System.out.println("执行切入点方法前记录日志");
  39. //2.获取方法执行所需的参数
  40. Object[] args = pjp.getArgs();
  41. //3.执行切入点方法
  42. rtValue = pjp.proceed(args);
  43. //后置通知
  44. System.out.println("正常执行切入点方法后记录日志");
  45. }catch (Throwable t){
  46. //异常通知
  47. System.out.println("执行切入点方法产生异常后记录日志"+t);
  48. }finally {
  49. //最终通知
  50. System.out.println("无论切入点方法执行是否有异常都记录日志");
  51. }
  52. return rtValue;
  53. }
  54. }
  • 创建配置类:config.SpringConfiguration

  1. package cn.itbluebox.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  5. @Configuration
  6. @ComponentScan("cn.itbluebox")
  7. @EnableAspectJAutoProxy
  8. public class SpringConfiguration {
  9. }
  • 创建测试类:

  1. package cn.itbluebox.test;
  2. import cn.itbluebox.config.SpringConfiguration;
  3. import cn.itbluebox.pojo.User;
  4. import cn.itbluebox.service.UserService;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. public class SpringAOPTest {
  7. public static void main(String[] args) {
  8. //1、获取容器
  9. AnnotationConfigApplicationContext
  10. ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
  11. //2、获取Bean对象
  12. UserService userService = ac.getBean("userService", UserService.class);
  13. //3、准备数据
  14. User user = new User();
  15. user.setId("1");
  16. user.setUsername("test");
  17. user.setNickname("张三");
  18. //4、执行方法
  19. userService.save(user);
  20. }
  21. }

运行测试类

4、可以将切入点精确到方法

  • 在接口当中创建update方法

  1. public void update(User user) {
  2. System.out.println("保存用户信息:"+user);
  3. }
  • 修改LogUtil

  1. @Pointcut("execution(* cn.itbluebox.service.impl.UserServiceImpl.save(..))")
  • 运行测试类

  • 修改SpringAOPTest

  1. package cn.itbluebox.test;
  2. import cn.itbluebox.config.SpringConfiguration;
  3. import cn.itbluebox.pojo.User;
  4. import cn.itbluebox.service.UserService;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. public class SpringAOPTest {
  7. public static void main(String[] args) {
  8. //1、获取容器
  9. AnnotationConfigApplicationContext
  10. ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
  11. //2、获取Bean对象
  12. UserService userService = ac.getBean("userService", UserService.class);
  13. //3、准备数据
  14. User user = new User();
  15. user.setId("1");
  16. user.setUsername("test");
  17. user.setNickname("张三");
  18. //4、执行方法
  19. userService.update(user);
  20. }
  21. }
  • 运行测试类(没有切入)

四、AOP常用注解分析

1、用于开启注解AOP支持

(1)@EnableAspectJAutoProxy
1)源码
  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Import(AspectJAutoProxyRegistrar.class)
  5. public @interface EnableAspectJAutoProxy {
  6. /** * Indicate whether subclass‐based (CGLIB) proxies are to be created as opposed * to standard Java interface‐based proxies. The default is {@code false}. */
  7. boolean proxyTargetClass() default false;
  8. /** * Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal} * for retrieval via the {@link org.springframework.aop.framework.AopContext} class. * Off by default, i.e. no guarantees that {@code AopContext} access will work. * @since 4.3.1 */
  9. boolean exposeProxy() default false;
  10. }
2)说明

作用:

  1. 表示开启spring对注解aop的支持。

它有两个属性,分别是指定采用的代理方式和
是否暴露代理对象,通过AopContext可以进行访问。

从定义可以看得出,它引入AspectJAutoProxyRegister.class对象,该对象是基于注解@EnableAspectJAutoProxy注册一个AnnotationAwareAspectJAutoProxyCreator,该对象通过调用

  1. AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

注册一个aop代理对象生成器。
关于AnnotationAwareAspectJAutoProxyCreator请参

属性:
proxyTargetClass:指定是否采用cglib进行代理。默认值是false,表示使用jdk的代理。
exposeProxy: 指定是否暴露代理对象,通过AopContext可以进行访问。

使用场景: 当我们注解驱动开发时,在需要使用aop实现某些功能的情况下,都需要用到此注解。

3)示例

  1. package cn.itbluebox.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  5. @Configuration
  6. @ComponentScan("cn.itbluebox")
  7. @EnableAspectJAutoProxy
  8. public class SpringConfiguration {
  9. }

2、用于配置切面的

(1)@Aspect
1)源码
  1. /** * Aspect declaration * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.TYPE)
  4. public @interface Aspect {
  5. /** * Per clause expression, defaults to singleton aspect * <p/> * Valid values are "" (singleton), "perthis(...)", etc */
  6. public String value() default "";
  7. }
2)说明

作用:声明当前类是一个切面类。

属性:
value:默认我们的切面类应该为单例的。但是当切面类为一个多例类时,指定预处理的切入点表达式。用法是perthis(切入点表达式)。

它支持指定切入点表达式,或者是用@Pointcut修饰的方法名称(要求全限定方法名)

使用场景:此注解也是一个注解驱动开发aop的必备注解。

3)示例
  1. @Component
  2. @Scope("prototype")//注意:通常情况下我们的切面类是不需要多例的。
  3. @Aspect(value="execution(* cn.itbluebox.service.impl.*.*(..))")
  4. public class LogUtil {
  5. /** * 用于配置当前方法是一个前置通知 */
  6. @Before("execution(* cn.itbluebox.service.impl.*.*(..))")
  7. public void printLog(){
  8. System.out.println("执行打印日志的功能");
  9. }
  10. }

3、用于配置切入点表达式的

(1)@Pointcut
1)源码
  1. /** * Pointcut declaration * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.METHOD)
  4. public @interface Pointcut {
  5. /** * The pointcut expression * We allow "" as default for abstract pointcut */
  6. String value() default "";
  7. /** * When compiling without debug info, or when interpreting pointcuts at runtime, * the names of any arguments used in the pointcut are not available. * Under these circumstances only, it is necessary to provide the arg names in * the annotation ‐ these MUST duplicate the names used in the annotated method. * Format is a simple comma‐separated list. */
  8. String argNames() default "";
  9. }
2)说明

作用:
此注解是用于指定切入点表达式的。
属性:
value:用于指定切入点表达式。

argNames:用于指定切入点表达式的参数。参数可以是execution中的,也可以是args中的。通常情况下不使用此属性也可以获得切入点方法参数。

使用场景:在实际开发中,当我们的多个通知需要执行,同时增强的规则确定的情况下,就可以把切入点表达式通用化。此注解就是代替xml中的<aop:pointcut>标签,实现切入点表达式的通用化。

3)示例

  1. /* Pointcut通用切入点表达式 execution允许 */
  2. @Pointcut("execution(* cn.itbluebox.service.impl.UserServiceImpl.save(..))")
  3. private void pt1(){}

4、用于配置通知的

(1)@Before
1)源码
  1. /** * Before advice * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.METHOD)
  4. public @interface Before {
  5. /** * The pointcut expression where to bind the advice */
  6. String value();
  7. /** * When compiling without debug info, or when interpreting pointcuts at runtime, * the names of any arguments used in the advice declaration are not available. * Under these circumstances only, it is necessary to provide the arg names in * the annotation ‐ these MUST duplicate the names used in the annotated method. * Format is a simple comma‐separated list. */
  8. String argNames() default "";
  9. }
2)说明

作用:
被此注解修饰的方法为前置通知。前置通知的执行时间点是在切入点方法执行之
前。
属性:
value:用于指定切入点表达式。可以是表达式,也可以是表达式的引用。
argNames:用于指定切入点表达式参数的名称。它要求和切入点表达式中的参数名称
一致。通常不指定也可以获取切入点方法的参数内容。
使用场景:在实际开发中,我们需要对切入点方法执行之前进行增强, 此时就用到了前置通
知。
在通知(增强的方法)中需要获取切入点方法中的参数进行处理时,就要配合切入点表达
式参数来使用。

3)示例
  1. /** * 前置通知 */
  2. @Before(value = "pt1(user)",argNames = "user")
  3. public void beforeLog(User user){
  4. System.out.println("执行切入点方法前记录日志"+user);
  5. }
(2)@AfterReturning
1)源码
  1. /** * After returning advice * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.METHOD)
  4. public @interface AfterReturning {
  5. /** * The pointcut expression where to bind the advice */
  6. String value() default "";
  7. /** * The pointcut expression where to bind the advice, overrides "value" when specified */
  8. String pointcut() default "";
  9. /** * The name of the argument in the advice signature to bind the returned value to */
  10. String returning() default "";
  11. /** * When compiling without debug info, or when interpreting pointcuts at runtime, * the names of any arguments used in the advice declaration are not available. * Under these circumstances only, it is necessary to provide the arg names in * the annotation ‐ these MUST duplicate the names used in the annotated method. * Format is a simple comma‐separated list. */
  12. String argNames() default "";
  13. }
2)说明

作用:
用于配置后置通知。
后置通知的执行是在切入点方法正常执行之后执行。
需要注意的是,由于基于注解的配置时,spring创建通知方法的拦截器链时,后置
通知在最终通知之后,所以会先执行@After注解修饰的方法。

属性:
value:用于指定切入点表达式,可以是表达式,也可以是表达式的引用。
pointcut:它的作用和value是一样的。
returning:指定切入点方法返回值的变量名称。它必须和切入点方法返回值名称一
致。
argNames:用于指定切入点表达式参数的名称。它要求和切入点表达式中的参数名称
一致。通常不指定也可以获取切入点方法的参数内容。
使用场景:此注解是用于配置后置增强切入点方法的。被此注解修饰方法会在切入点方法正常执行之后执行。
在我们实际开发中,像提交事务,记录访问日志,统计方法执行效率等等都可以利用后置通知实现。

  1. User findById(String id);

  1. public User findById(String id) {
  2. System.out.println("切入点方法开始执行。。。");
  3. User user = new User();
  4. user.setId(id);
  5. user.setUsername("itbluebox");
  6. user.setNickname("蓝盒子");
  7. return user;
  8. }
  • 修改LogUtil

  1. /* 后置通知 */
  2. @AfterReturning(value = "execution(* cn.itbluebox.service.impl.*.* (..))&&args(param)",
  3. returning = "user")
  4. public void afterReturningLog(String param,Object user){
  5. System.out.println("-----------------------");
  6. System.out.println("正常执行切入点方法后记录日志,切入点方法的参数 是:"+param);
  7. System.out.println("正常执行切入点方法后记录日志,切入点方法的返回值 是:"+user);
  8. System.out.println("-----------------------");
  9. }
  • 修改测试类

  1. package cn.itbluebox.test;
  2. import cn.itbluebox.config.SpringConfiguration;
  3. import cn.itbluebox.pojo.User;
  4. import cn.itbluebox.service.UserService;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. public class SpringAOPTest {
  7. public static void main(String[] args) {
  8. //1、获取容器
  9. AnnotationConfigApplicationContext
  10. ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
  11. //2、获取Bean对象
  12. UserService userService = ac.getBean("userService", UserService.class);
  13. //3、准备数据
  14. User user = new User();
  15. user.setId("1");
  16. user.setUsername("test");
  17. user.setNickname("张三");
  18. //4、执行方法
  19. userService.findById("1");
  20. }
  21. }

(3)@AfterThrowing
1)源码
  1. /** * After throwing advice * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.METHOD)
  4. public @interface AfterThrowing {
  5. /** * The pointcut expression where to bind the advice */
  6. String value() default "";
  7. /** * The pointcut expression where to bind the advice, overrides "value" when specified */
  8. String pointcut() default "";
  9. /** * The name of the argument in the advice signature to bind the thrown exception to */
  10. String throwing() default "";
  11. /** * When compiling without debug info, or when interpreting pointcuts at runtime, * the names of any arguments used in the advice declaration are not available. * Under these circumstances only, it is necessary to provide the arg names in * the annotation ‐ these MUST duplicate the names used in the annotated method. * Format is a simple comma‐separated list. */
  12. String argNames() default "";
  13. }
2)说明

作用:
用于配置异常通知。
属性:

value:用于指定切入点表达式,可以是表达式,也可以是表达式的引用。

pointcut:它的作用和value是一样的。

throwing:指定切入点方法执行产生异常时的异常对象变量名称。它必须和异常变量名称一致。

argNames:用于指定切入点表达式参数的名称。它要求和切入点表达式中的参数名称一致。
通常不指定也可以获取切入点方法的参数内容。

使用场景:用此注解修饰的方法执行时机是在切入点方法执行产生异常之后执行。

3)示例

  1. package cn.itbluebox.service.impl;
  2. import cn.itbluebox.pojo.User;
  3. import cn.itbluebox.service.UserService;
  4. import org.springframework.stereotype.Service;
  5. @Service("userService")
  6. public class UserServiceImpl implements UserService {
  7. public void save(User user) {
  8. System.out.println("保存用户信息:"+user);
  9. }
  10. public void update(User user) {
  11. System.out.println("保存用户信息:"+user);
  12. }
  13. public User findById(String id) {
  14. System.out.println("切入点方法开始执行。。。");
  15. User user = new User();
  16. user.setId(id);
  17. user.setUsername("itbluebox");
  18. user.setNickname("蓝盒子");
  19. int i = 1/0;
  20. return user;
  21. }
  22. }

(3)@After
1)源码
  1. /** * After finally advice * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.METHOD)
  4. public @interface After {
  5. /** * The pointcut expression where to bind the advice */
  6. String value();
  7. /** * When compiling without debug info, or when interpreting pointcuts at runtime, * the names of any arguments used in the advice declaration are not available. * Under these circumstances only, it is necessary to provide the arg names in * the annotation ‐ these MUST duplicate the names used in the annotated method. * Format is a simple comma‐separated list. */
  8. String argNames() default "";
  9. }
2)说明

作用:
用于指定最终通知。
属性:
value:用于指定切入点表达式,可以是表达式,也可以是表达式的引用。
argNames:用于指定切入点表达式参数的名称。它要求和切入点表达式中的参数名称
一致。通常不指定也可以获取切入点方法的参数内容。
使用场景:最终通知的执行时机,是在切入点方法执行完成之后执行,无论切入点方法执行是
否产生异常最终通知都会执行。所以被此注解修饰的方法,通常都是做一些清理操作。

3)示例
  1. /** * 最终通知 */
  2. @After(value = "execution(* cn.itbluebox.service.impl.*.*(..))")
  3. public void afterLog(){
  4. System.out.println("无论切入点方法执行是否有异常都记录日志");
  5. }
(4)@Around
1)源码
  1. /** * Around advice * * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a> */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.METHOD)
  4. public @interface Around {
  5. /** * The pointcut expression where to bind the advice */
  6. String value();
  7. /** * When compiling without debug info, or when interpreting pointcuts at runtime, * the names of any arguments used in the advice declaration are not available. * Under these circumstances only, it is necessary to provide the arg names in * the annotation ‐ these MUST duplicate the names used in the annotated method. * Format is a simple comma‐separated list. */
  8. String argNames() default "";
  9. }
2)说明

作用:用于指定环绕通知。

属性:
value:用于指定切入点表达式,可以是表达式,也可以是表达式的引用。
argNames:用于指定切入点表达式参数的名称。
它要求和切入点表达式中的参数名称一致。通常不指定也可以获取切入点方法的参数内容。

使用场景:环绕通知有别于前面介绍的四种通知类型。它不是指定增强方法执行时机的,而是
spring为我们提供的一种可以通过编码的方式手动控制增强方法何时执行的机制。

3)示例
  1. /** * 环绕通知 */
  2. @Around("execution(* com.itheima.service.impl.*.*(..))")
  3. public Object arountPrintLog(ProceedingJoinPoint pjp){
  4. //1.定义返回值
  5. Object rtValue = null;
  6. try{
  7. //前置通知
  8. System.out.println("执行切入点方法前记录日志");
  9. //2.获取方法执行所需的参数
  10. Object[] args = pjp.getArgs();
  11. //3.执行切入点方法
  12. rtValue = pjp.proceed(args);
  13. //后置通知
  14. System.out.println("正常执行切入点方法后记录日志");
  15. }catch (Throwable t){
  16. //异常通知
  17. System.out.println("执行切入点方法产生异常后记录日志");
  18. }finally {
  19. //最终通知
  20. System.out.println("无论切入点方法执行是否有异常都记录日志");
  21. }
  22. return rtValue;
  23. }

五、用于扩展目标类的

1、@DeclareParents

(1)源码
  1. /** * Declare parents mixin annotation */
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.FIELD)
  4. public @interface DeclareParents {
  5. /** * The target types expression */
  6. String value();
  7. /** * Optional class defining default implementation * of interface members (equivalent to defining * a set of interface member ITDs for the * public methods of the interface). */
  8. Class defaultImpl() default DeclareParents.class;
  9. // note ‐ a default of "null" is not allowed,
  10. // hence the strange default given above.
  11. }
(2)说明

作用:
用于给被增强的类提供新的方法。(实现新的接口)

属性:
value:用于指定目标类型的表达式。当在全限定类名后面跟上+时,表示当前类及其子类

defaultImpl:指定提供方法或者字段的默认实现类。

使用场景:当我们已经完成了一个项目的某个阶段开发,此时需要对已完成的某个类加入一些新的方法,我们首先想到的是写一个接口,然后让这些需要方法的类实现此接口,但是如果目标类非常复杂,牵一发而动全身,改动的话可能非常麻烦。
此时就可以使用此注解,然后建一个代理类,同时代理该类和目标类。

(3)示例

  1. package cn.itbluebox.service;
  2. import cn.itbluebox.pojo.User;
  3. public interface ValidateService {
  4. boolean checkUser(User user);
  5. }

  1. package cn.itbluebox.service.impl;
  2. import cn.itbluebox.pojo.User;
  3. import cn.itbluebox.service.ValidateService;
  4. public class ValidateServiceImpl implements ValidateService {
  5. @Override
  6. public boolean checkUser(User user) {
  7. if(user.getNickname().contains("孙子")){
  8. return false;
  9. }
  10. return true;
  11. }
  12. }
  • 创建MyPointcut

  1. package cn.itbluebox.pointcut;
  2. import org.aspectj.lang.annotation.Pointcut;
  3. public class MyPointcut {
  4. /** * 用于定义通用的切入点表达式 */
  5. @Pointcut(value = "execution(* cn.itbluebox.service.impl.*.*(..))")
  6. public void pointcut1(){}
  7. }
  • AOP切面类的配置

  1. package cn.itbluebox.utils;
  2. import cn.itbluebox.pojo.User;
  3. import cn.itbluebox.service.ValidateService;
  4. import cn.itbluebox.service.impl.ValidateServiceImpl;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.*;
  7. import org.springframework.stereotype.Component;
  8. @Component
  9. @Aspect
  10. public class LogUtil {
  11. @DeclareParents(value =
  12. "cn.itbluebox.service.UserService+", defaultImpl =
  13. ValidateServiceImpl.class)
  14. private ValidateService validateService;
  15. /** * 用于配置当前方法是一个前置通知 */
  16. @Before(value = "cn.itbluebox.pointcut.MyPointcut.pointcut1() && args(user) && this(validateService)")
  17. public void printLog(User user, ValidateService validateService) {
  18. //第二种触发方式
  19. boolean check = validateService.checkUser(user);
  20. if (check) {
  21. System.out.println("执行打印日志的功能");
  22. } else {
  23. throw new IllegalStateException("名称非法");
  24. }
  25. }
  26. /* Pointcut通用切入点表达式 execution允许 */
  27. @Pointcut("execution(* cn.itbluebox.service.impl.*.*(..))")
  28. private void pt1() {
  29. }
  30. /* 前置通知 */
  31. @Before("pt1()")
  32. public void beforeLog() {
  33. System.out.println("执行切入点方法前记录日志");
  34. }
  35. /* 后置通知 */
  36. @AfterReturning(value = "execution(* cn.itbluebox.service.impl.*.* (..))&&args(param)",
  37. returning = "user")
  38. public void afterReturningLog(String param, Object user) {
  39. System.out.println("-----------------------");
  40. System.out.println("正常执行切入点方法后记录日志,切入点方法的参数 是:" + param);
  41. System.out.println("正常执行切入点方法后记录日志,切入点方法的返回值 是:" + user);
  42. System.out.println("-----------------------");
  43. }
  44. /* 异常通知 */
  45. @AfterReturning("pt1()")
  46. public void afterThrowingLog() {
  47. System.out.println("执行切入点方法产生异常后记录日志");
  48. }
  49. /* 最终通知 */
  50. @After("pt1()")
  51. public void afterLog() {
  52. System.out.println("无论切入点方法执行是否异常都记录日志");
  53. }
  54. /* 环绕通知 */
  55. @Around("pt1()")
  56. public Object aroundPrintLog(ProceedingJoinPoint pjp) {
  57. //1.定义返回值
  58. Object rtValue = null;
  59. try {
  60. //前置通知
  61. System.out.println("执行切入点方法前记录日志");
  62. //2.获取方法执行所需的参数
  63. Object[] args = pjp.getArgs();
  64. //3.执行切入点方法
  65. rtValue = pjp.proceed(args);
  66. //后置通知
  67. System.out.println("正常执行切入点方法后记录日志");
  68. } catch (Throwable t) {
  69. //异常通知
  70. System.out.println("执行切入点方法产生异常后记录日志" + t);
  71. } finally {
  72. //最终通知
  73. System.out.println("无论切入点方法执行是否有异常都记录日志");
  74. }
  75. return rtValue;
  76. }
  77. }
  • 修改SpringAOPTest

  1. package cn.itbluebox.test;
  2. import cn.itbluebox.config.SpringConfiguration;
  3. import cn.itbluebox.pojo.User;
  4. import cn.itbluebox.service.UserService;
  5. import cn.itbluebox.service.ValidateService;
  6. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  7. public class SpringAOPTest {
  8. public static void main(String[] args) {
  9. //1、获取容器
  10. AnnotationConfigApplicationContext
  11. ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
  12. //2、获取Bean对象
  13. UserService userService = ac.getBean("userService", UserService.class);
  14. //3、准备数据
  15. User user = new User();
  16. user.setId("1");
  17. user.setUsername("test");
  18. user.setNickname("孙子");
  19. //4、执行方法
  20. userService.save(user);
  21. }
  22. }

2、@EnableLoadTimeWeaving

(1)源码
  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Import(LoadTimeWeavingConfiguration.class)
  5. public @interface EnableLoadTimeWeaving {
  6. /** * Whether AspectJ weaving should be enabled. */
  7. AspectJWeaving aspectjWeaving() default AspectJWeaving.AUTODETECT;
  8. /** * AspectJ weaving enablement options. */
  9. enum AspectJWeaving {
  10. /** * Switches on Spring‐based AspectJ load‐time weaving. */
  11. ENABLED,
  12. /** * Switches off Spring‐based AspectJ load‐time weaving (even if a * "META‐INF/aop.xml" resource is present on the classpath). */
  13. DISABLED,
  14. /** * Switches on AspectJ load‐time weaving if a "META‐INF/aop.xml" resource * is present in the classpath. If there is no such resource, then AspectJ * load‐time weaving will be switched off. */
  15. AUTODETECT;
  16. }
  17. }
(2)说明

作用:
用于切换不同场景下实现增强。
属性:
aspectjWeaving:是否开启LTW的支持。
ENABLED 开启LTW
DISABLED 不开启LTW
AUTODETECT 如果类路径下能读取到META‐INF/aop.xml文件,则开启LTW,否则关闭
使用场景:在Java 语言中,从织入切面的方式上来看,存在三种织入方式:编译期织入、类加载期织入和运行期织入。

编译期织入是指在Java编译期,采用特殊的编译器,将切面织入到Java类中;而类加载期织入则指通过特殊的类加载器,在类字节码加载到JVM时,织入切面;运行期织入则是采用CGLib工具或JDK动态代理进行切面的织入。

AspectJ提供了两种切面织入方式,第一种通过特殊编译器,在编译期,将AspectJ语言编写的切面类织入到Java类中,可以通过一个Ant或Maven任务来完成这个操作;第二种方式是类加载期织入,也简称为LTW(Load Time Weaving)

(3)示例
  1. **
  2. * @author 黑马程序员
  3. * @Company http://www.itheima.com
  4. */
  5. //@Component
  6. @Aspect
  7. public class LoadTimeWeavingAspect {
  8. /** * 增强方法 * @param pjp * @return * @throws Throwable */
  9. @Around("pointcut()")
  10. public Object profile(ProceedingJoinPoint pjp) throws Throwable {
  11. //1.创建秒表对象
  12. StopWatch sw = new StopWatch(getClass().getSimpleName());
  13. try {
  14. //2.记录执行
  15. sw.start(pjp.getSignature().getName());
  16. //3.调用切入点方法并返回
  17. return pjp.proceed();
  18. } finally {
  19. //4.停止计时
  20. sw.stop();
  21. //5.输出
  22. System.out.println(sw.prettyPrint());
  23. }
  24. }
  25. /** * 切入点表达式 */
  26. @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
  27. public void pointcut() {
  28. }
  29. }

相关文章

最新文章

更多