AOP切入点表达式有哪五种通知类型

Spring中的Aop AOP(面向切面编程)是Spring框架的核心概念之一,它通过代理模式在不修改原始代码的基础上实现功能增强。AOP的核心概念包括:连接点(程序执行的方法或异常点)、切入点(需要增强的具体方法)、通知(增强功能的实现方法)、通知类(存放通知的类)以及切面(描述通知与切入点的对应关系)。通过AOP,开发者可以无侵入式地为特定方法添加功能,如计算执行时间等,体现了Spring无入侵式的设计理念。 阅读详情

针对AOP中切入表达式我会分三个方面去讲解,分别是 语法格式 、 通配符和 书写技巧 。

1.1 语法格式

首先我们先要明确两个概念:

  • 切入点:要进行增强的方法

  • 切入点表达式:要进行增强的方法的描述方式

对于切入点的描述,我们其实是有两种方式的,先来看下面的例子

描述方式一:执行com.itheima.dao包下的BookDao接口中的无参数update方法

<pre class="hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">execution(void com.itheima.dao.BookDao.update())</pre>

描述方式二:执行com.itheima.dao.impl包下的BookDaoImpl类中的无参数update方法

<pre class="hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">execution(void com.itheima.dao.impl.BookDaoImpl.update())</pre>

因为调用接口方法的时候最终运行的还是其实现类的方法,所以上面两种描述方式都是可以的。

对于切入点表达式的语法为:

  • 切入点表达式标准格式:动作关键字(访问修饰符 返回值 包名.类/接口名.方法名(参数) 异常名)

对于这个格式,我们不需要硬记,通过一个例子,理解它:

<pre class="hljs lisp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">execution(public User com.itheima.service.UserService.findById(int))</pre>
  • execution:动作关键字,描述切入点的行为动作,例如execution表示执行到指定切入点

  • public:访问修饰符,还可以是public,private等,可以省略

  • User:返回值,写返回值类型

  • com.itheima.service:包名,多级包使用点连接

  • UserService:类/接口名称

  • findById:方法名

  • int:参数,直接写参数的类型,多个类型用逗号隔开

  • 异常名:方法定义中抛出指定异常,可以省略

切入点表达式就是要找到需要增强的方法,所以它就是对一个具体方法的描述,但是方法的定义会有很多,所以如果每一个方法对应一个切入点表达式,想想这块就会觉得将来编写起来会比较麻烦,有没有更简单的方式呢?

就需要用到下面的通配符。

1.2 通配符

我们使用通配符描述切入点,主要的目的就是简化之前的配置,具体都有哪些通配符可以使用?

  • * :单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现
    <pre class="hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">execution(public * com.itheima.*.UserService.find*(*))</pre>
匹配com.itheima包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法
  • .. :多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写
    <pre class="hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">execution(public User com..UserService.findById(..))</pre>
匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法
  • + :专用于匹配子类类型
    <pre class="hljs markdown" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">execution(* *..*Service+.*(..))</pre>
这个使用率较低,描述子类的,咱们做Java开发,继承机会就一次,使用都很慎重,所以很少用它。*Service+,表示所有以Service结尾的接口的子类。

接下来,我们把使用到的切入点表达式来分析下:

<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">execution(void com.itheima.dao.BookDao.update())
匹配接口,能匹配到
execution(void com.itheima.dao.impl.BookDaoImpl.update())
匹配实现类,能匹配到
execution(* com.itheima.dao.impl.BookDaoImpl.update())
返回值任意,能匹配到
execution(* com.itheima.dao.impl.BookDaoImpl.update(*))
返回值任意,但是update方法必须要有一个参数,无法匹配,要想匹配需要在update接口和实现类添加参数
execution(void com.*.*.*.*.update())
返回值为void,com包下的任意包三层包下的任意类的update方法,匹配到的是实现类,能匹配
execution(void com.*.*.*.update())
返回值为void,com包下的任意两层包下的任意类的update方法,匹配到的是接口,能匹配
execution(void *..update())
返回值为void,方法名是update的任意包下的任意类,能匹配
execution(* *..*(..))
匹配项目中任意类的任意方法,能匹配,但是不建议使用这种方式,影响范围广
execution(* *..u*(..))
匹配项目中任意包任意类下只要以u开头的方法,update方法能满足,能匹配
execution(* *..*e(..))
匹配项目中任意包任意类下只要以e结尾的方法,update和save方法能满足,能匹配
execution(void com..*())
返回值为void,com包下的任意包任意类任意方法,能匹配,*代表的是方法
execution(* com.itheima.*.*Service.find*(..))
将项目中所有业务层方法的以find开头的方法匹配
execution(* com.itheima.*.*Service.save*(..))
将项目中所有业务层方法的以save开头的方法匹配</pre>

后面两种更符合我们平常切入点表达式的编写规则

1.3 书写技巧

对于切入点表达式的编写其实是很灵活的,那么在编写的时候,有没有什么好的技巧让我们用用:

  • 所有代码按照标准规范开发,否则以下技巧全部失效

  • 描述切入点 通 **常描述接口 **,而不描述实现类,如果描述到实现类,否则就出现紧耦合了

  • 访问控制修饰符针对接口开发均采用public描述( **可省略访问控制修饰符描述 **)

  • 返回值类型对于增删改类使用精准类型加速匹配,对于查询类使用 * 通配快速描述

  • **包名 **书写 **尽量不使用..匹配 **,效率过低,常用 * 做单个包描述匹配,或精准匹配

  • **接口名/类名 **书写名称与模块相关的 **采用 * 匹配 **,例如UserService书写成 * Service,绑定业务层接口名

  • **方法名 **书写以 **动词 **进行 **精准匹配 **,名词采用 *匹配,例如getById书写成getBy *,selectAll书写成selectAll

  • 参数规则较为复杂,根据业务方法灵活调整

  • 通常 **不使用 异常 **作为 **匹配 **规则

二、AOP通知类型

它所代表的含义是将 通知 添加到 切入点 方法执行的 前面 。

除了这个注解外,还有没有其他的注解,换个问题就是除了可以在前面加,能不能在其他的地方加?

2.1 类型介绍

我们先来回顾下AOP通知:

  • AOP通知描述了抽取的共性功能,根据共性功能抽取的位置不同,最终运行代码时要将其加入到合理的位置

通知具体要添加到切入点的哪里?

共提供了5种通知类型:

  • 前置通知

  • 后置通知

  • 环绕通知(重点)

  • 返回后通知(了解)

  • 抛出异常后通知(了解)

为了更好的理解这几种通知类型,我们来看一张图

(1)前置通知,追加功能到方法执行前,类似于在代码1或者代码2添加内容

(2)后置通知,追加功能到方法执行后,不管方法执行的过程中有没有抛出异常都会执行,类似于在代码5添加内容

(3)返回后通知,追加功能到方法执行后,只有方法正常执行结束后才进行,类似于在代码3添加内容,如果方法执行抛出异常,返回后通知将不会被添加

(4)抛出异常后通知,追加功能到方法抛出异常后,只有方法执行出异常才进行,类似于在代码4添加内容,只有方法抛出异常后才会被添加

(5)环绕通知,环绕通知功能比较强大,它可以追加功能到方法执行的前后,这也是比较常用的方式,它可以实现其他四种通知类型的功能,具体是如何实现的,需要我们往下学习。

2.2 环境准备

  • 创建一个Maven项目

  • pom.xml添加Spring依赖

    <pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"><dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.4</version>
        </dependency>
    </dependencies></pre>
  • 添加BookDao和BookDaoImpl类
    <pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">public interface BookDao {
        public void update();
        public int select();
    }
    ​
    @Repository
    public class BookDaoImpl implements BookDao {
        public void update(){
            System.out.println("book dao update ...");
        }
        public int select() {
            System.out.println("book dao select is running ...");
            return 100;
        }
    }</pre>
  • 创建Spring的配置类
    <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@Configuration
    @ComponentScan("com.itheima")
    @EnableAspectJAutoProxy
    public class SpringConfig {
    }</pre>
  • 创建通知类
    <pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@Component
    @Aspect
    public class MyAdvice {
        @Pointcut("execution(void com.itheima.dao.BookDao.update())")
        private void pt(){}
    ​
        public void before() {
            System.out.println("before advice ...");
        }
    ​
        public void after() {
            System.out.println("after advice ...");
        }
    ​
        public void around(){
            System.out.println("around before advice ...");
            System.out.println("around after advice ...");
        }
    ​
        public void afterReturning() {
            System.out.println("afterReturning advice ...");
        }

        public void afterThrowing() {
            System.out.println("afterThrowing advice ...");
        }
    }</pre>
  • 编写App运行类
    <pre class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">public class App {
        public static void main(String[] args) {
            ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
            BookDao bookDao = ctx.getBean(BookDao.class);
            bookDao.update();
        }
    }</pre>

最终创建好的项目结构如下:

2.3 通知类型的使用

前置通知

修改MyAdvice,在before方法上添加 @Before注解

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Before("pt()")
    //此处也可以写成 @Before("MyAdvice.pt()"),不建议
    public void before() {
        System.out.println("before advice ...");
    }
}</pre>

后置通知

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Before("pt()")
    public void before() {
        System.out.println("before advice ...");
    }
    @After("pt()")
    public void after() {
        System.out.println("after advice ...");
    }
}</pre>

 

环绕通知

基本使用

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Around("pt()")
    public void around(){
        System.out.println("around before advice ...");
        System.out.println("around after advice ...");
    }
}</pre>

运行结果中,通知的内容打印出来,但是原始方法的内容却没有被执行。

因为环绕通知需要在原始方法的前后进行增强,所以环绕通知就必须要能对原始操作进行调用,具体如何实现?

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Around("pt()")
    public void around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("around before advice ...");
        //表示对原始操作的调用
        pjp.proceed();
        System.out.println("around after advice ...");
    }
}</pre>

**说明: **proceed()为什么要抛出异常?

主要原因原始方法不清楚到底执行会不会有异常,所以直接先抛出异常。原因很简单,看下源码就知道了

再次运行,程序可以看到原始方法已经被执行了

注意事项

(1)原始方法有返回值的处理

  • 修改MyAdvice,对BookDao中的select方法添加环绕通知,
<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Pointcut("execution(int com.itheima.dao.BookDao.select())")
    private void pt2(){}

    @Around("pt2()")
    public void aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around before advice ...");
        //表示对原始操作的调用
        pjp.proceed();
        System.out.println("around after advice ...");
    }
}</pre>
  • 修改App类,调用select方法
<pre class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = ctx.getBean(BookDao.class);
        int num = bookDao.select();
        System.out.println(num);
    }
}</pre>

运行后会报错,错误内容为:

Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.itheima.dao.BookDao.select() at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226) at com.sun.proxy.$Proxy19.select(Unknown Source) at com.itheima.App.main(App.java:12)

错误大概的意思是: 空的返回不匹配原始方法的int返回

  • void就是返回Null

  • 原始方法就是BookDao下的select方法

所以如果我们使用环绕通知的话,要根据原始方法的返回值来设置环绕通知的返回值,具体解决方案为:

<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Pointcut("execution(int com.itheima.dao.BookDao.select())")
    private void pt2(){}

    @Around("pt2()")
    public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around before advice ...");
        //表示对原始操作的调用
        Object ret = pjp.proceed();
        System.out.println("around after advice ...");
        return ret;
    }
}</pre>

说明:

为什么返回的是Object而不是int的主要原因是Object类型更通用。 所以更一般的写法环绕通知的返回类型写object而不是void,如果没有返回值,那么object就为空

在环绕通知中是可以对原始方法返回值就行修改的。

返回后通知

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Pointcut("execution(int com.itheima.dao.BookDao.select())")
    private void pt2(){}

    @AfterReturning("pt2()")
    public void afterReturning() {
        System.out.println("afterReturning advice ...");
    }
}</pre>

**注意: **返回后通知是需要在原始方法 select 正常执行后才会被执行,如果 select() 方法执行的过程中出现了异常,那么返回后通知是不会被执行。后置通知是不管原始方法有没有抛出异常都会被执行。

异常后通知

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    @Pointcut("execution(int com.itheima.dao.BookDao.select())")
    private void pt2(){}

    @AfterReturning("pt2()")
    public void afterThrowing() {
        System.out.println("afterThrowing advice ...");
    }
}</pre>

**注意: **异常后通知是需要原始方法抛出异常,可以在 select() 方法中添加一行代码 int i = 1/0 即可。如果没有抛异常,异常后通知将不会被执行。

介绍完这5种通知类型,我们来思考下环绕通知是如何实现其他通知类型的功能的?

因为环绕通知是可以控制原始方法执行的,所以我们把增强的代码写在调用原始方法的不同位置就可以实现不同的通知类型的功能,如:

通知类型总结

知识点1:@After

名称@After
类型方法注解
位置通知方法定义上方
作用设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法后运行

知识点2:@AfterReturning

名称@AfterReturning
类型方法注解
位置通知方法定义上方
作用设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法正常执行完毕后执行

知识点3:@AfterThrowing

名称@AfterThrowing
类型方法注解
位置通知方法定义上方
作用设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法运行抛出异常后执行

知识点4:@Around

名称@Around
类型方法注解
位置通知方法定义上方
作用设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前后运行

环绕通知注意事项

  1. 环绕通知必须依赖形参ProceedingJoinPoint才能实现对原始方法的调用,进而实现原始方法调用前后同时添加通知

  2. 通知中如果未使用ProceedingJoinPoint对原始方法进行调用将跳过原始方法的执行

  3. 对原始方法的调用可以不接收返回值,通知方法设置成void即可,如果接收返回值,最好设定为Object类型

  4. 原始方法的返回值如果是void类型,通知方法的返回值类型可以设置成void,也可以设置成Object

  5. 由于无法预知原始方法运行后是否会抛出异常,因此环绕通知方法必须要处理Throwable异常,推荐直接抛出异常

华硕三频万兆分布式路由组网实战:从AiMesh 2.0到全屋Wi-Fi 6覆盖 Mesh组网是解决大户型Wi-Fi覆盖难题的主流方案,其核心原理是通过多个节点构建网状网络,实现设备间的无缝漫游。在技术层面,Wi-Fi 6标准、三频设计(包括专属回程频段)和有线回程等技术,共同提升了网络的带宽、稳定性和抗干扰能力。这些技术的工程价值在于,能够为多设备并发、高带宽应用(如4K/8K流媒体、在线游戏、内网文件高速传输)提供可靠支撑。具体到应用场景,例如利用华硕AiMesh 2.0系统,配合三频万兆分布式路由器,可以实现智能的设备连接与负载均衡。通过合理规划节点位置、优先采用有线回程,并进行Q 阅读详情

相关推荐

DBeaver连接IoTDB避坑指南:手动配置JDBC驱动的完整流程(附问题排查)

本文提供了在DBeaver中手动配置Apache IoTDB JDBC驱动的完整流程与避坑指南。详细解析了从驱动创建、库文件添加(关键是指向整个lib目录)到连接测试的每一步,并重点针对常见的ClassNotFoundException、连接拒绝等错误提供了深度排查方案,帮助用户高效建立图形化管理连接。

weixin_28327051的博客 396

java版云笔记(六)之AOP

今天主要是利用aop技术追加service的响应时间的计算和异常的日志记录。 AOP   AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。面向对象编程将程序分解成各个层次的对象,面向切面编程将程序运行过程分解成各个切面。   AOP从程序运行角度考虑程序...

weixin_30916125的博客 202

BSW: Dcm模块配置--28服务

这个章节讲解下UDS中的28服务(Communcation Control)的配置,顾名思义,对当前ECU的通讯状态进行变更,主要设置应用报文,网络管理报文的收发控制,通常应用在刷写APP软件时,降低总线负载。这里首先要提醒的一点是,28服务几乎不需要手写代码,只有在客户需求要求前置条件不满足时报特定NRC时才需要填充代码。实际配置中主要涉及的模块有DCM,BSWM,ComM,RTE.2.BswM模块的配置。3.ComM模块的配置。1.DCM模块的配置。

Forever_Oct的博客 3086

深入理解Java中的AOP环绕通知——pjp.proceed()的作用与用法

在这篇博客中,我们将深入探讨AOP环绕通知中最关键的一行代码:Object ret = pjp.proceed();,并解释它的作用与用法。

weixin_62079735的博客 5794

汇川EASY521 PLC实战指南:从硬件选型到程序架构与故障排查

可编程逻辑控制器(PLC)是工业自动化系统的核心大脑,其工作原理基于循环扫描机制,持续读取输入、执行用户程序并更新输出。这种确定性控制方式,使其在逻辑控制、顺序控制和运动控制中具有不可替代的技术价值,广泛应用于机械设备、生产线和过程控制等场景。对于汇川EASY521这类紧凑型高性能PLC,其核心优势在于集成了运动控制与丰富通讯接口,为中小型设备提供了高性价比的一站式解决方案。在实际应用中,工程师常需处理如“汇川sv660n”伺服驱动器的集成调试,或利用“hslcommunication”等开源库实现C#上位

weixin_42525264的博客 743

别再只用线性回归了!用sklearn的核岭回归搞定非线性数据(附RBF核调参实战)

本文详细介绍了如何使用sklearn的核岭回归处理非线性数据,特别是RBF核的应用与调参实战。通过网格搜索找到最佳参数组合,核岭回归能有效捕捉数据中的复杂波动,显著提升模型性能。适合需要解决非线性问题的数据科学家和机器学习工程师。

weixin_34246529的博客 339

日志 aop 中 @Around(“execution(?)“)中路径的配置

//环绕通知 首先是:包名 然后是: 类名 然后是方法名:方法名 括号内是:参数 @Around("execution(* com.sinosoft.*.controller.*.*(..))") public Object around(ProceedingJoinPoint point) throws Throwable{ //参数是目标方法 try { String typeName=""; String locat=""; Signature signature = po

CYY941027的博客 1万+

告别测序污染:手把手教你用Decontam+SCRUB+FEAST完成微生物组数据清洗全流程

本文详细解析了使用Decontam、SCRUB和FEAST三大工具完成微生物组数据清洗的全流程。从环境准备、数据导入到污染物识别、样本间污染校正及溯源分析,提供了一套可复用的污染校正工作流,帮助研究者提升数据质量,确保后续分析的可靠性。

weixin_33462927的博客 317

AOP中环绕通知为何返回类型选用Object?

在AOP中,环绕通知是一项强大而灵活的功能,可以在目标方法执行前后以及异常抛出时进行拦截和控制。为了适应各种可能的返回值类型,环绕通知的返回类型被定义为Object,使其具备足够的灵活性。在使用环绕通知时,我们需要根据目标方法的返回值类型进行适当的类型转换和处理,以确保程序的正确性。通过合理的使用环绕通知,我们可以更好地实现面向切面编程,提高代码的可维护性和可扩展性。

weixin_62079735的博客 526

YOLO26 电子产品零部件质检:缺陷检测 + Streamlit

YOLO26s 电子质检链路:1100 张工业零部件图 (4 类缺陷) → 增强 (blur+shear+低光照) → 150 epoch → mAP@0.5=0.91 → Streamlit 质检界面 (上传/摄像头双模式) → 缺陷严重度分级 (pass/minor/major/critical)。核心贡献:blur+shear 模拟产线振动和视角偏移, 使模糊场景 recall 从 72% 提升至 78%;Streamlit 一键部署, 无需前端经验。建议先收集 200 张纯正常件做负样本防过检;

m0_71589036的博客 377

【YOLOv12多模态融合改进】| TFAM:时序融合注意力模块 | 引入通道 - 空间双分支注意力机制,解决双模态特征融合中时序关联不足的问题

TFAM模块通过时序信息驱动的注意力机制,解决了双时相特征融合中时序关联不足的问题,实现了更精准的变化区域定位和特征表示。其结构轻量、泛化性强,为变化检测任务提供了一种高效的特征融合解决方案。

Limiiiing的博客 868

SpringAOP使用

aop老是忘,面试也老是提到,再详细记录下 基本术语: 通俗解释: 基于XML配置实现AOP: 包结构: XML配置文件:具体使用注释写的很详细 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ...

天上掉下两毛钱 868

Android 车载开发深度解析:CarPropertyManager 核心机制与实践指南

摘要: Android Automotive OS中,CarPropertyManager作为车载信息娱乐系统的核心组件,连接应用层与车辆硬件(如车速、油量等),其架构基于分层设计(App→CarAPI→VHAL→ECU),采用发布-订阅模式实现高效数据通信。通过HIDL/AIDL跨进程交互及共享内存优化,延迟可降至2ms以下。安全机制包括分级权限控制(VISIBLE/PROTECTED/SYSTEM_RESTRICTED)和HMAC防篡改。开发时需注意监听器生命周期管理、事件合并降负载及低功耗适配。

郑伟强dev的专栏 344

Java开发学习(十六)----AOP切入点表达式及五种通知类型解析

对于AOP中切入点表达式,总共有三个大的方面,分别是、和。1.1 语法格式首先我们先要明确两个概念:切入点:要进行增强的方法切入点表达式:要进行增强的方法的描述方式对于切入点的描述,我们其实是有两种方式的,先来看下面的例子描述方式一:执行com.itheima.dao包下的BookDao接口中的无参数update方法 描述方式二:执行com.itheima.dao.impl包下的BookDaoImpl类中的无参数update方法 因为调用接口方法的时候最终运行的还是其实现类的方法,所以上面两种描述方

437

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

注解方式使用Spring AOP,五种通知类型以及切入点表达式

注解方式使用Spring AOP,完善中…

布先生 945

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

xml方式使用Spring AOP,五种通知类型以及切入点表达式

切入点表达式 定义公共的切入点表达式 xml里定义 注解方式定义 通知类型 xml方式使用Spring AOP 注解方式使用Spring AOP &amp;amp;amp;lt;bean id=&amp;amp;quot;myXmlAspect&amp;amp;quot; class=&amp;amp;quot;cn.yooyo.aop.MyXmlAspect&amp;amp;quot;/&amp;amp;amp;gt; &amp;amp;amp;

布先生 474

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

JavaWeb——AOP进阶(1/5)-通知类型(介绍,五种通知的执行时机,代码实战说明,关于环绕通知,@Pointcut抽取切入点表达式)

AOP 提供五种通知,需根据业务需要灵活选择。 @Around 是最灵活的通知方式,执行控制最强。 切入点表达式可通过 @Pointcut 抽取复用,减少重复代码。 不同类型的通知适用于不同阶段的增强逻辑插入,测试时可观察执行顺序加深理解。

li13437542099的博客 578

idea__Spring5框架13——基于XML的AOP———五种常用通知类型,配切入点表达式(新标签)

四种常用通知类型,配切入点表达式(标签)一、四种常用通知类型二、配置切入点表达式三、最后一种通知类型:环绕通知 涉及到的新标签: <aop:after-returning>, <aop:after-throwing>, <aop:after>,<aop:pointcut> 一、四种常用通知类型 我们修改一下Logger类,添加多三个方法。 接着,我们配置一下四个通知,用到了三个新的标签。 <aop:after-returning>, <ao

weixin_42858422的博客 662

Spring AOP 第二篇:深入通知类型、通知顺序与切入点表达式

在上一期中,我们了解了 AOP 的基本概念和核心组件。这一期,我们将深入探讨其中最关键的两个部分:通知(Advice)的类型、通知顺序和切入点(Pointcut)的表达式。

h41256的博客 1389

【Spring】AOP切入点表达式&通知类型&数据获取(二)

在上一篇我们学习了SpringAOP使用以及核心概念,本篇一起来学习下AOP切入点表达式&通知类型&数据获取

观止study 1692

什么是AOP?

AOP(Aspect Oriented Programming)面向切面编程,一种编程范式,指导开发者如何组织程序结构OOP(Object Oriented Programming)面向对象编程作用:在不惊动原始设计的基础上为其进行功能增强。简单的说就是在不改变方法源代码的基础上对方法进行功能增强。Spring理念:无入侵式/无侵入式。

m0_70619994的博客 7040

spring-AOP切入点表达式和通知类型

在 Spring AOP 中,切入点表达式(Pointcut Expression)是用来定义哪些方法会被拦截和增强的。切入点表达式通过 execution、within、args 等关键字来匹配目标方法。  在 Spring AOP 中,通知(Advice) 是指在切点方法执行时要执行的增强逻辑。Spring AOP 提供了五种不同类型的通知,它们决定了增强逻辑执行的时机。我们将详细介绍这五种通知类型和一个具体案例

m0_71804943的博客 1146

代码优雅升级,提升开发效率:挖掘Spring AOP配置的学习宝藏!

AOP通知描述了抽取的共性功能,根据共性功能抽取的位置不同,最终运行代码时要将其加入到合理的位置通知具体要添加到切入点的哪里?前置通知后置通知环绕通知(重点)返回后通知(了解)抛出异常后通知(了解)为了更好的理解这几种通知类型,我们来看一张图(1)前置通知,追加功能到方法执行前,类似于在代码1或者代码2添加内容(2)后置通知,追加功能到方法执行后,不管方法执行的过程中有没有抛出异常都会执行,类似于在代码5添加内容。

m0_59230408的博客 4万+

Spring之AOP的切点、通知、切点表达式以及知识要点

Spring之AOP的切点、通知、切点表达式以及知识要点

楠黎倾风的博客 1万+

AOP切面编程的理解

一、什么是Spring的AOP? AOP在spring中又叫“面向切面编程”,它可以说是对传统我们面向对象编程的一个补充,从字面上顾名思义就可以知道,它的主要操作对象就是“切面”,所以我们就可以简单的理解它是贯穿于方法之中,在方法执行前、执行时、执行后、返回值后、异常后要执行的操作。相当于是将我们原本一条线执行的程序在中间切开加入了一些其他操作一样。 在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能应用在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块...

Ywh1211的博客 2482

【Spring(十一)】万字带你深入学习面向切面编程AOP

今天我们来学习AOP,在最初我们学习Spring时说过Spring的两大特征,一个是IOC,一个是AOP,我们现在要学习的就是这个AOP。面向切面编程,一种编程范式,指导开发者如何组织程序结构。作用:在不惊动原始设计的基础上为其进行功能增强。首先我们先来看看代码环境,在主方法中获取BookDao对象,并调用它的save()方法,在中save()方法是测试它的万次执行效率,而此类中的别的方法没有这个功能👇👇。类。

hihielite的博客 5438
上一篇: 云原生Java框架有哪些呢
下一篇: Java绘机制和绘图演示
Java_ttcd
博客等级 码龄4年 198粉丝 190原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值