Spring——AOP中五大通知功能的执行顺序(完全注解开发)
Spring大家肯定都用过的,IOC和AOP是它的两大核心,那么这篇文章主要和大家分享一下如果对一个方法声明了切面、添加了五种通知,它们的执行顺序是怎样的呢?由于之前学Spring的时候写过太多了xml配置了,所以这篇文章采用完全注解开发的方式来实现。
Spring大家肯定都用过的,IOC和AOP是它的两大核心,那么这篇文章主要和大家分享一下如果对一个方法声明了切面、添加了五种通知,它们的执行顺序是怎样的呢?
由于之前学Spring的时候写过太多了xml配置了,所以这篇文章采用完全注解开发的方式来实现。
首先是pom文件,我们添加所需依赖。
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>5.2.5.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> <version>5.2.5.RELEASEversion> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> <scope>testscope> 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; @Servicepublic 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@Componentpublic 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中的 @ComponentScan(basePackages = "com.szh")//开启aop自动代理功能,相当于xml中的 @EnableAspectJAutoProxypublic 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); }}
https://blog.csdn.net/weixin_43823808/article/details/125460170