Spring AOP注解配置中,如何解决通知执行顺序混乱问题?

2026-05-16 07:272阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计540个文字,预计阅读时间需要3分钟。

Spring AOP注解配置中,如何解决通知执行顺序混乱问题?

今天在测试Spring的AOP时,发现使用注解配置AOP的方式会导致通知的执行顺序混乱。最终通知虽然自然地在异常通知之前执行,但测试代码如下(1)定义目标接口:

javapublic interface TargetInterface { 接口方法;}

今天在测试Spring的AOP时,发现使用注解配置AOP的方式会导致通知的执行顺序紊乱。

测试代码

(1)定义TargetInterface目标接口

public interface TargetInterface { public abstract void targetProxy(); }

(2)定义TargetImpl目标类

@Component("target") public class TargetImpl implements TargetInterface { public void targetProxy() { System.out.println("target proxy ......"); int i = 1/0;//异常 } }

(3)定义切面类(内含增强方法)

@Component("myAspect")//定义切面类 @Aspect//声明当前类是切面类 public class TargetAspect { //定义切点表达式 @Pointcut("execution(* com.ahzyy.target.impl.*.*(..))") public void pt() { } @Before("pt()") public void before() { System.out.println("前置通知......"); } @After("pt()") public void after() { System.out.println("最终通知......"); } @AfterReturning("pt()") public void afterReturning() { System.out.println("后置通知......"); } @AfterThrowing("pt()") public void afterThrowing() { System.out.println("异常通知......"); } }

(4)配置applicationContextAnno.xml文件

<!--配置组件扫描的包--> <context:component-scan base-package="com.ahzyy"/> <!--配置AOP自动代理--> <aop:aspectj-autoproxy/>

(5)定义测试类

@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration("classpath:applicationContext.xml") @ContextConfiguration("classpath:applicationContextAnno.xml") public class AopTest { @Autowired private TargetInterface target; @Test public void test01() { target.targetProxy(); } }

(6)运行结果:


(7)解决方法:

(7.1)使用xml配置方式配置AOP;

(7.2)注解使用@Around(环绕通知)方式配置AOP(修改TargetAspect类使用环绕通知);

Spring AOP注解配置中,如何解决通知执行顺序混乱问题?

@Component("myAspect")//定义切面类 @Aspect//声明当前类是切面类 public class TargetAspect { //定义切点表达式 @Pointcut("execution(* com.ahzyy.target.impl.*.*(..))") public void pt() { } @Around("pt()") public Object aroundNotice(ProceedingJoinPoint pjp) { System.out.println("环绕通知"); Object result = null; before();//前置通知 try { result = pjp.proceed(); afterReturning();//后置通知 } catch (Throwable throwable) { afterThrowing();//异常通知 throwable.printStackTrace(); } after();//最终通知 return result; } public void before() { System.out.println("前置通知......"); } public void afterReturning() { System.out.println("后置通知......"); } public void afterThrowing() { System.out.println("异常通知......"); } public void after() { System.out.println("最终通知......"); } }

(7.3)运行结果

[运行顺序正确]

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

本文共计540个文字,预计阅读时间需要3分钟。

Spring AOP注解配置中,如何解决通知执行顺序混乱问题?

今天在测试Spring的AOP时,发现使用注解配置AOP的方式会导致通知的执行顺序混乱。最终通知虽然自然地在异常通知之前执行,但测试代码如下(1)定义目标接口:

javapublic interface TargetInterface { 接口方法;}

今天在测试Spring的AOP时,发现使用注解配置AOP的方式会导致通知的执行顺序紊乱。

测试代码

(1)定义TargetInterface目标接口

public interface TargetInterface { public abstract void targetProxy(); }

(2)定义TargetImpl目标类

@Component("target") public class TargetImpl implements TargetInterface { public void targetProxy() { System.out.println("target proxy ......"); int i = 1/0;//异常 } }

(3)定义切面类(内含增强方法)

@Component("myAspect")//定义切面类 @Aspect//声明当前类是切面类 public class TargetAspect { //定义切点表达式 @Pointcut("execution(* com.ahzyy.target.impl.*.*(..))") public void pt() { } @Before("pt()") public void before() { System.out.println("前置通知......"); } @After("pt()") public void after() { System.out.println("最终通知......"); } @AfterReturning("pt()") public void afterReturning() { System.out.println("后置通知......"); } @AfterThrowing("pt()") public void afterThrowing() { System.out.println("异常通知......"); } }

(4)配置applicationContextAnno.xml文件

<!--配置组件扫描的包--> <context:component-scan base-package="com.ahzyy"/> <!--配置AOP自动代理--> <aop:aspectj-autoproxy/>

(5)定义测试类

@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration("classpath:applicationContext.xml") @ContextConfiguration("classpath:applicationContextAnno.xml") public class AopTest { @Autowired private TargetInterface target; @Test public void test01() { target.targetProxy(); } }

(6)运行结果:


(7)解决方法:

(7.1)使用xml配置方式配置AOP;

(7.2)注解使用@Around(环绕通知)方式配置AOP(修改TargetAspect类使用环绕通知);

Spring AOP注解配置中,如何解决通知执行顺序混乱问题?

@Component("myAspect")//定义切面类 @Aspect//声明当前类是切面类 public class TargetAspect { //定义切点表达式 @Pointcut("execution(* com.ahzyy.target.impl.*.*(..))") public void pt() { } @Around("pt()") public Object aroundNotice(ProceedingJoinPoint pjp) { System.out.println("环绕通知"); Object result = null; before();//前置通知 try { result = pjp.proceed(); afterReturning();//后置通知 } catch (Throwable throwable) { afterThrowing();//异常通知 throwable.printStackTrace(); } after();//最终通知 return result; } public void before() { System.out.println("前置通知......"); } public void afterReturning() { System.out.println("后置通知......"); } public void afterThrowing() { System.out.println("异常通知......"); } public void after() { System.out.println("最终通知......"); } }

(7.3)运行结果

[运行顺序正确]

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