AOP的异常通知
一、配置异常通知的步骤 (Aspectj方式)
1、只有当切点报异常才能触发异常通知
2、在spring中有Aspectj 方式提供了异常通知方法
2.1 如果希望通过 schema-base 实现需要按照特定的要求自己编写方法
3、实现步骤:
3.1 新建类,在类中写任意名称的方法
public class myExcetion {
public void myexcetion(Exception e1){
System.out.println("执行异常,tain"+e1.getMessage());
}
}
3.2 在spring配置文件中配置
3.2.1 <aop:aspect ref="">:ref= 表示在哪个类中
3.2.2 <aop:xxxx> 什么标签表示什么通知
3.2.3 method:表示当触发这个通知时,调用哪个方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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"> <bean id="myexcetion" class="com.bjsxt.myExcetion.myexcetion"></bean>
<aop:config>
<aop:aspect ref="myexcetion">
<aop:pointcut expression="execution(* com.bjsxt.test.Test.demo1())" id="mypoint"/>
<aop:after-throwing method="myExcetion" pointcut-ref="mypoint" throwing="e1"/> throwing 表示异常处理的方法的参数名 (可以不再异常通知中声明异常对象)
</aop:aspect>
</aop:config>
<bean id="test" class="com.bjsxt.test.Test"></bean>
</beans>
二、使用Schema-base 实现异常
1、新建一个类实现ThrowsAdvice接口,且必须使用afterThrowing这个方法名。。
1.1有两种参数方式
必须有 1个 或 4个参数
1.2 异常类型要与切点报的异常类型一致,
public class Mythrow implements ThrowsAdvice{
// public void afterThrowing(Exception ex) throws Throwable {
// System.out.println("执行异常通知11111");
//}
public void afterThrowing(Method m, Object[] args, Object target, Exception ex) {
System.out.println("执行异常通知2222");
}
}
2、在applicationContext.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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"> <bean id="mythrow" class="com.bjsxt.advice.Mythrow"></bean>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint"/>
</aop:config>
</beans>
AOP的异常通知的更多相关文章
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- Spring 通过来AOP 实现前置,环绕,异常通知,注解(转)
本节主要内容: 1. Spring AOP前置通知案例 2. Spring AOP环绕通知案例 3. Spring AOP异常通知案例 4. Spring AOP注解使 ...
- aop编程之后置通知,环绕通知和异常通知
---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html 具体流程如下: 1. ...
- [转载] Spring框架——AOP前置、后置、环绕、异常通知
通知类型: 步骤: 1. 定义接口 2. 编写对象(被代理对象=目标对象) 3. 编写通知(前置通知目标方法调用前调用) 4. 在beans.xml文件配置 4.1 配置 被代理对象=目标对象 4.2 ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等
实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...
- :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式
三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...
随机推荐
- Python_变量命名
Python的变量命名 变量的命名的原则一直都是我这种小白的头疼事,好几条,根本记不住...... 为了解决大家的头疼问题,今天想出来一个好办法,那就是:身边常备头疼片.......(哈哈哈,开玩笑的 ...
- xshell常用的快捷键
删除ctrl + d 删除光标所在位置上的字符相当于VIM里x或者dlctrl + h 删除光标所在位置前的字符相当于VIM里hx或者dhctrl + k 删除光标后面所 ...
- [剑指offer]51-数组中的逆序对(归并排序)
题目链接 https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5 题意 在数组中的两个数字,如果前面一个数 ...
- 小白鼠排队(map容器插入数据的四种方法)
题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...
- 关于echarts堆叠图标问题 ,某条数数不需要堆叠的处理
当直接访问的总量不需要堆叠的时候,将stack改为tiled即可,效果图如下
- php 多进程 父进程的阻塞与非阻塞
php中进程的阻塞,主要是父进程等待子进程退出. 1.php代码如下: <?php //定义进程数量 define('FORK_NUMS', 5); //用于保存进程pid $pids = ar ...
- win下Apache2.4的下载与安装
1.到apache官网上下载apache的安装文件 http://httpd.apache.org/download.cgi 点击链接Files for Microsoft Windows,因为a ...
- 将文件转换成byte[]数组
代码 /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl&quo ...
- js去除字符串空格(空白符)
使用js去除字符串内所带有空格,有以下三种方法: ( 1 ) replace正则匹配方法 去除字符串内所有的空格:str = str.replace(/\s*/g,""); 去除字 ...
- 让 div中的div垂直居中的方法!!同样是抄袭来的(*^__^*)
同样 ,水平居中很简单,给子div设置margin:0px auto; 垂直居中也不难::给父div设置display:table-cell;vertical-align:middle; 重点是dis ...