欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring-AOP @AspectJ进阶之绑定抛出的异常

发布时间:2025/3/21 javascript 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring-AOP @AspectJ进阶之绑定抛出的异常 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

  • 概述
  • 实例
  • 总结

概述

和通过切点函数绑定连接点信息不同,连接点抛出的异常必须使用AfterThrowing注解的throwing成员进行绑定


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

业务类

package com.xgj.aop.spring.advisor.aspectJAdvance.bindException;import org.springframework.stereotype.Component;@Component public class BussinessException {public void dealBussiness(String bussinessName) {System.out.println("dealBussiness executed");// just a demo code ,in fact it's not cautiousif (bussinessName != null && "bug".equals(bussinessName))throw new IllegalArgumentException("iae Exception");elsethrow new RuntimeException("re Exception");} }

切面

package com.xgj.aop.spring.advisor.aspectJAdvance.bindException;import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: BindReturnValueAspect* * @Description: @Aspect标注的切面,* 和通过切点函数绑定连接点信息不同,连接点抛出的异常必须使用AfterThrowing注解的throwing成员进行绑定* * (1)处throwing指定的异常名和(2)处入参的异常名相同,这个异常增强只在连接点抛出的异常instanceof* IllegalArgumentException才匹配,增强方法通过iae参数可以访问抛出的异常对象。* * @author: Mr.Yang* * @date: 2017年9月12日 下午5:47:23*/@Aspect public class BindExceptionAspect {// (1)@AfterThrowing(value = "target(com.xgj.aop.spring.advisor.aspectJAdvance.bindException.BussinessException)", throwing = "iae")public void crossCuttingCode(IllegalArgumentException iae) {// (2)System.out.println("----bindException()----");System.out.println("exception:" + iae.getMessage());System.out.println("----bindException()----");} }

(1)处throwing指定的异常名和(2)处入参的异常名相同,这个异常增强只在连接点抛出的异常instanceof IllegalArgumentException才匹配,增强方法通过iae参数可以访问抛出的异常对象。


配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件 (2)扫描类包以及应用注解定义的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindException"/><!-- 基于@AspectJ切面的驱动器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面类 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindException.BindExceptionAspect"/></beans>

单元测试

package com.xgj.aop.spring.advisor.aspectJAdvance.bindException;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BindExceptionAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindException/conf-bindException.xml");ctx.getBean("bussinessException", BussinessException.class).dealBussiness("bug");} }

输出结果

2017-09-12 20:26:25,344 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3695de1a: startup date [Tue Sep 12 20:26:25 BOT 2017]; root of context hierarchy 2017-09-12 20:26:25,458 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindException/conf-bindException.xml] dealBussiness executed ----bindException()---- exception:iae Exception ----bindException()----

可见当sdealBussiness(“bug”)抛出异常后,异常增强起效,处理完成后,再向外抛出IllegalArgumentException。如果将①处的代码调整为dealBussiness(“bug2”)后,再运行代码,将只看到异常输出的信息,异常增强没有任何动作,这是因为RuntimeException 不按类型匹配于 IllegalArgumentException,切点不匹配。


总结

  • 通过切点复合运算,你可以定义出各种复杂的切点,使切点表达式的能力进一步提升。

  • 你可以直接使用切点复合运算符对切点函数进行运算,也可以通过切点名引用其它命名切点。

  • 当对同一个连接点织入多个增强时,你必须考虑让切面类实现Ordered接口,此外还必须合理计划同一个切面类中增强方法的声明顺序,因为这些信息都会影响到增强的织入顺序。

  • 在@AspectJ的切点表达式中,大多数的切点函数都可以绑定连接点方法的入参,以便增强方法访问连接点信息。

  • 此外,你也可以简单地将增强方法的第一个入参定义为JoinPoint访问连接点的上下文。

总结

以上是生活随笔为你收集整理的Spring-AOP @AspectJ进阶之绑定抛出的异常的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。