sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Spring——AOP中五大通知功能的执行顺序(完全注解开发)

1.前言

Spring大家肯定都用过的,IOC和AOP是它的两大核心,那么这篇文章主要和大家分享一下如果对一个方法声明了切面、添加了五种通知,它们的执行顺序是怎样的呢?

由于之前学Spring的时候写过太多了xml配置了,所以这篇文章采用完全注解开发的方式来实现。


2.项目源码

首先是pom文件,我们添加所需依赖。

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
 
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

接下来我们来一个业务接口以及它的实现类。

package com.szh.service;
 
/**
 *
 */
public interface SomeService {
 
    String doSome(String name,Integer age);
 
}

由于我们不写xml配置,所以需要在业务接口实现类上添加 @Service 注解,通过注解将这个类交给Spring IOC容器管理。 

package com.szh.service.impl;
 
import com.szh.service.SomeService;
import org.springframework.stereotype.Service;
 
@Service
public class SomeServiceImpl implements SomeService {
 
    @Override
    public String doSome(String name, Integer age) {
        //int a = 10 / 0; //用于测试异常通知
        System.out.println("业务方法doSome(),创建商品的订单");
        return "姓名:" + name + ",年龄:" + age;
    }
 
}

然后是我们的切面类,同上面的业务实现类,也需要使用 @Component 注解将这个类交给Spring IOC容器管理。 

package com.szh.handle;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
 
@Aspect
@Component
public class MyAspect {
 
    @Before(value = "myPointCut()")
    public void myBefore() {
        System.out.println("前置通知,在目标方法之前先执行的....");
    }
 
    @AfterReturning(value = "myPointCut()", returning = "obj")
    public void myAfterReturning(Object obj) {
        System.out.println("后置通知,在目标方法之后再执行的(如有异常,则后置通知不会执行).... 目标方法返回值:" + obj);
    }
 
    @Around(value = "myPointCut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知中前置通知的功能....");
        Object obj = joinPoint.proceed();
        System.out.println("环绕通知中后置通知的功能....");
        return obj;
    }
 
    @AfterThrowing(value = "myPointCut()", throwing = "e")
    public void myAfterThrowing(Exception e) {
        System.out.println("异常通知,在目标方法抛出异常时执行的,异常原因是:" + e.getMessage());
    }
 
    @After(value = "myPointCut()")
    public void myAfter() {
        System.out.println("最终通知,总是会被执行的....");
    }
 
    @Pointcut(value = "execution(* com.szh.service.impl.*.do*(..))")
    private void myPointCut() {
        //无需代码
    }
}

最后再来个配置类,在这个配置类中开启要替代xml配置文件的相关功能。

package com.szh.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
/**
 *
 */
@Configuration
//开启包扫描功能,相当于xml中的 <context:component-scan base-package="com.szh"></context:component-scan>
@ComponentScan(basePackages = "com.szh")
//开启aop自动代理功能,相当于xml中的 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@EnableAspectJAutoProxy
public class MyConfig {
}

最后是测试类。

package com.szh;
 
import com.szh.config.MyConfig;
import com.szh.service.SomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
/**
 *
 */
public class MyTest {
 
    @Test
    public void testAnnotationAop() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        SomeService service = (SomeService) context.getBean("someServiceImpl");
        String info = service.doSome("张起灵", 20);
        System.out.println(info);
    }
}

2.1 无异常的执行结果

2.2 有异常的执行结果

https://blog.csdn.net/weixin_43823808/article/details/125460170
posted on   sunny123456  阅读(284)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示