温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

浅谈spring aop的五种通知类型

发布时间:2020-08-23 07:19:20 来源:脚本之家 阅读:287 作者:C__joy 栏目:编程语言

spring aop通知(advice)分成五类: 

前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。 

正常返回通知[After returning advice]:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行。 

异常返回通知[After throwing advice]:在连接点抛出异常后执行。 

返回通知[After (finally) advice]:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。 

环绕通知[Around advice]:环绕通知围绕在连接点前后,比如一个方法调用的前后。这是最强大的通知类型,能在方法调用前后自定义一些操作。

环绕通知还需要负责决定是继续处理join point(调用ProceedingJoinPoint的proceed方法)还是中断执行。 
接下来通过编写示例程序来测试一下五种通知类型:

定义接口

package com.chenqa.springaop.example.service;

public interface BankService {

  /**
   * 模拟的银行转账
   * @param from 出账人
   * @param to 入账人
   * @param account 转账金额
   * @return
   */
  public boolean transfer(String form, String to, double account);
}

编写实现类

package com.chenqa.springaop.example.service.impl;

import com.chenqa.springaop.example.service.BankService;

public class BCMBankServiceImpl implements BankService {

  public boolean transfer(String form, String to, double account) {
    if(account<100) {
      throw new IllegalArgumentException("最低转账金额不能低于100元");
    }
    System.out.println(form+"向"+to+"交行账户转账"+account+"元");
    return false;
  }

}

修改spring配置文件,添加以下内容:

<!-- bankService bean -->  
  <bean id="bankService" class="com.chenqa.springaop.example.service.impl.BCMBankServiceImpl"/>
  <!-- 切面 -->
  <bean id="myAspect" class="com.chenqa.springaop.example.aspect.MyAspect"/>
  <!-- aop配置 -->
  <aop:config>
    <aop:aspect ref="myAspect">
      <aop:pointcut expression="execution(* com.chenqa.springaop.example.service.impl.*.*(..))" id="pointcut"/>
      <aop:before method="before" pointcut-ref="pointcut"/>
      <aop:after method="after" pointcut-ref="pointcut"/>
      <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
      <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
      <aop:around method="around" pointcut-ref="pointcut"/>
    </aop:aspect>
  </aop:config>

编写测试程序

ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
    BankService bankService = context.getBean("bankService", BankService.class);
    bankService.transfer("张三", "李四", 200);

执行后输出: 

浅谈spring aop的五种通知类型 

将测试程序中的200改成50,再执行后输出: 

浅谈spring aop的五种通知类型 

通过测试结果可以看出,五种通知的执行顺序为:

前置通知→环绕通知→正常返回通知/异常返回通知→返回通知,可以多次执行来查看。

情况一: 一个方法只被一个Aspect类拦截

当一个方法只被一个Aspect拦截时,这个Aspect中的不同advice是按照怎样的顺序进行执行的呢?请看:

添加 PointCut类

该pointcut用来拦截test包下的所有类中的所有方法。

package test;

import org.aspectj.lang.annotation.Pointcut;

public class PointCuts {
  @Pointcut(value = "within(test.*)")
  public void aopDemo() {

  }
}

添加Aspect类

该类中的advice将会用到上面的pointcut,使用方法请看各个advice的value属性。

package test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Aspect1 {

  @Before(value = "test.PointCuts.aopDemo()")
  public void before(JoinPoint joinPoint) {
    System.out.println("[Aspect1] before advise");
  }

  @Around(value = "test.PointCuts.aopDemo()")
  public void around(ProceedingJoinPoint pjp) throws Throwable{
    System.out.println("[Aspect1] around advise 1");
    pjp.proceed();
    System.out.println("[Aspect1] around advise2");
  }

  @AfterReturning(value = "test.PointCuts.aopDemo()")
  public void afterReturning(JoinPoint joinPoint) {
    System.out.println("[Aspect1] afterReturning advise");
  }

  @AfterThrowing(value = "test.PointCuts.aopDemo()")
  public void afterThrowing(JoinPoint joinPoint) {
    System.out.println("[Aspect1] afterThrowing advise");
  }

  @After(value = "test.PointCuts.aopDemo()")
  public void after(JoinPoint joinPoint) {
    System.out.println("[Aspect1] after advise");
  }
}

添加测试用Controller

添加一个用于测试的controller,这个controller中只有一个方法,但是它会根据参数值的不同,会作出不同的处理:一种是正常返回一个对象,一种是抛出异常(因为我们要测试@AfterThrowing这个advice)

package test;

import test.exception.TestException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/aop")
public class AopTestController {

  @ResponseStatus(HttpStatus.OK)
  @RequestMapping(value = "/test", method = RequestMethod.GET)
  public Result test(@RequestParam boolean throwException) {
    // case 1
    if (throwException) {
      System.out.println("throw an exception");
      throw new TestException("mock a server exception");
    }

    // case 2
    System.out.println("test OK");
    return new Result() {{
      this.setId(111);
      this.setName("mock a Result");
    }};
  }

  public static class Result {
    private int id;
    private String name;

    public int getId() {
      return id;
    }

    public void setId(int id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
  }
}

测试 正常情况

在浏览器直接输入以下的URL,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false1

我们会看到输出的结果是:

[Aspect1] around advise 1
[Aspect1] before advise
test OK
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise

测试 异常情况

在浏览器中直接输入以下的URL,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true1

我们会看到输出的结果是:

[Aspect1] around advise 1
[Aspect1] before advise
throw an exception
[Aspect1] after advise
[Aspect1] afterThrowing advise

结论

在一个方法只被一个aspect类拦截时,aspect类内部的 advice 将按照以下的顺序进行执行:

正常情况: 

浅谈spring aop的五种通知类型

异常情况: 

浅谈spring aop的五种通知类型

情况二: 同一个方法被多个Aspect类拦截

此处举例为被两个aspect类拦截。 

有些情况下,对于两个不同的aspect类,不管它们的advice使用的是同一个pointcut,还是不同的pointcut,都有可能导致同一个方法被多个aspect类拦截。那么,在这种情况下,这多个Aspect类中的advice又是按照怎样的顺序进行执行的呢?请看:

pointcut类保持不变

添加一个新的aspect类

package test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Aspect2 {

  @Before(value = "test.PointCuts.aopDemo()")
  public void before(JoinPoint joinPoint) {
    System.out.println("[Aspect2] before advise");
  }

  @Around(value = "test.PointCuts.aopDemo()")
  public void around(ProceedingJoinPoint pjp) throws Throwable{
    System.out.println("[Aspect2] around advise 1");
    pjp.proceed();
    System.out.println("[Aspect2] around advise2");
  }

  @AfterReturning(value = "test.PointCuts.aopDemo()")
  public void afterReturning(JoinPoint joinPoint) {
    System.out.println("[Aspect2] afterReturning advise");
  }

  @AfterThrowing(value = "test.PointCuts.aopDemo()")
  public void afterThrowing(JoinPoint joinPoint) {
    System.out.println("[Aspect2] afterThrowing advise");
  }

  @After(value = "test.PointCuts.aopDemo()")
  public void after(JoinPoint joinPoint) {
    System.out.println("[Aspect2] after advise");
  }
}

测试用Controller也不变

还是使用上面的那个Controller。但是现在 aspect1 和 aspect2 都会拦截该controller中的方法。

下面继续进行测试!

测试 正常情况

在浏览器直接输入以下的URL,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false1

我们会看到输出的结果是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] before advise
test OK
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise

但是这个时候,我不能下定论说 aspect2 肯定就比 aspect1 先执行。 

不信?你把服务务器重新启动一下,再试试,说不定你就会看到如下的执行结果:

[Aspect1] around advise 1
[Aspect1] before advise
[Aspect2] around advise 1
[Aspect2] before advise
test OK
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise

也就是说,这种情况下, aspect1 和 aspect2 的执行顺序是未知的。那怎么解决呢?不急,下面会给出解决方案。

测试 异常情况

在浏览器中直接输入以下的URL,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true1

我们会看到输出的结果是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] before advise
throw an exception
[Aspect1] after advise
[Aspect1] afterThrowing advise
[Aspect2] after advise
[Aspect2] afterThrowing advise

同样地,如果把服务器重启,然后再测试的话,就可能会看到如下的结果:

[Aspect1] around advise 1
[Aspect1] before advise
[Aspect2] around advise 1
[Aspect2] before advise
throw an exception
[Aspect2] after advise
[Aspect2] afterThrowing advise
[Aspect1] after advise
[Aspect1] afterThrowing advise

也就是说,同样地,异常情况下, aspect1 和 aspect2 的执行顺序也是未定的。

那么在 情况二 下,如何指定每个 aspect 的执行顺序呢? 

方法有两种:

  1. 实现org.springframework.core.Ordered接口,实现它的getOrder()方法
  2. 给aspect添加@Order注解,该注解全称为:org.springframework.core.annotation.Order

不管采用上面的哪种方法,都是值越小的 aspect 越先执行。 

比如,我们为 apsect1 和 aspect2 分别添加 @Order 注解,如下:

@Order(5)
@Component
@Aspect
public class Aspect1 {
  // ...
}

@Order(6)
@Component
@Aspect
public class Aspect2 {
  // ...
}

这样修改之后,可保证不管在任何情况下, aspect1 中的 advice 总是比 aspect2 中的 advice 先执行。如下图所示: 

浅谈spring aop的五种通知类型

注意点

如果在同一个 aspect 类中,针对同一个 pointcut,定义了两个相同的 advice(比如,定义了两个 @Before),那么这两个 advice 的执行顺序是无法确定的,哪怕你给这两个 advice 添加了 @Order 这个注解,也不行。这点切记。

对于@Around这个advice,不管它有没有返回值,但是必须要方法内部,调用一下 pjp.proceed();否则,Controller 中的接口将没有机会被执行,从而也导致了 @Before这个advice不会被触发。比如,我们假设正常情况下,执行顺序为”aspect2 -> apsect1 -> controller”,如果,我们把 aspect1中的@Around中的 pjp.proceed();给删掉,那么,我们看到的输出结果将是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise

从结果可以发现, Controller 中的 接口 未被执行,aspect1 中的 @Before advice 也未被执行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI