前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring-AOP @AspectJ进阶之绑定抛出的异常

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

作者头像
小小工匠
发布2021-08-17 10:03:32
4430
发布2021-08-17 10:03:32
举报
文章被收录于专栏:小工匠聊架构

文章目录

概述

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


实例

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

这里写图片描述
这里写图片描述

业务类

代码语言:javascript
复制
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 cautious
		if (bussinessName != null && "bug".equals(bussinessName))
			throw new IllegalArgumentException("iae Exception");
		else
			throw new RuntimeException("re Exception");
	}
}

切面

代码语言:javascript
复制
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参数可以访问抛出的异常对象。


配置文件

代码语言:javascript
复制
<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.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context 
       	http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindException"/>


<aop:aspectj-autoproxy proxy-target-class="true"/>
 

<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindException.BindExceptionAspect"/>

beans>

单元测试

代码语言:javascript
复制
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 {
	@Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindException/conf-bindException.xml");

		ctx.getBean("bussinessException", BussinessException.class)
				.dealBussiness("bug");
	}
}

输出结果

代码语言:javascript
复制
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访问连接点的上下文。
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/09/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 概述
  • 实例
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档