一、配置异常通知的步骤 (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的异常通知的更多相关文章

  1. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知

    本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知     1. Spring AOP  前置通知 XML配置使用案例     2. Spring AOP   ...

  3. Spring 通过来AOP 实现前置,环绕,异常通知,注解(转)

    本节主要内容:     1. Spring AOP前置通知案例     2. Spring AOP环绕通知案例     3. Spring AOP异常通知案例     4. Spring AOP注解使 ...

  4. aop编程之后置通知,环绕通知和异常通知

    ---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html  具体流程如下: 1. ...

  5. [转载] Spring框架——AOP前置、后置、环绕、异常通知

    通知类型: 步骤: 1. 定义接口 2. 编写对象(被代理对象=目标对象) 3. 编写通知(前置通知目标方法调用前调用) 4. 在beans.xml文件配置 4.1 配置 被代理对象=目标对象 4.2 ...

  6. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  7. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  8. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  9. :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式

    三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...

随机推荐

  1. vue element upload上传、清除等

    如果项目中可以使用file-list,那我们可以点击file-list删除文件列表: 有时候项目中是不要这个文件列表的,所以在上传成功以后,文件列表一直存在,要重新上传就必须刷新页面,所以我们需要手动 ...

  2. WebSphere Application Server中manageprofiles的使用

    转自 https://www.cnblogs.com/lgfeng/archive/2013/02/21/2921215.html ---------------------------------- ...

  3. 转化为分组背包 zoj 3769

    题目链接:https://vjudge.net/problem/ZOJ-3769 题意:现在你要去打怪,你有13种装备,每件装备会有伤害和防御两种属性,一般来说,每种装备只可以装备一件,但是特别的,戒 ...

  4. Python+Selenium学习--前进和后退

    场景 这两个功能一般不太常用.所能想到的场景大概也就是在几个页面间来回跳转,省去每次都get url. 代码 #!/usr/bin/env python # -*- coding:utf-8 -*- ...

  5. oracle 中从某天到某天一天一次执行某个函数

    DECLAREv_days INTEGER;v_start VARCHAR2(10);v_end VARCHAR2(10);v_3 INTEGER;v_enddays date;begin v_sta ...

  6. ES6之Promise对象

    创建Promise对象 function getHtml(url) { return new Promise((resolve, reject) => { let xhr = new XMLHt ...

  7. Django的视图函数和路由系统中一些没有用过的小点

    1.request对象 print("返回用户访问的url,但是不包括域名",request.path_info) print("返回请求的方法,全大写",re ...

  8. Django具体操作(四)

    自定义模板语法的标签 首先在django的项目中创建app并且在settings中添加了APP的名称. 在app添加templatetags模块(名字是固定的,也就是说,必须要这样) 如图如何自定义呢 ...

  9. javaweb导出excel

    百度找了半天也没找到一个提供有效思路的,全都告诉我此路不通 html表格数据粘贴到txt,然后改后缀为xsl,打开,发现二者无缝对接 @参考文章.@参考前任项目 /** * @todo * @para ...

  10. Union and Intersection of two sorted lists 并集和交集

    跟面试官确认是arrayList还是singly-linked list /*  Union 并集:两个升序的list a, b, 返回其并集(升序排序)*/ public class UnionTw ...