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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)