一、配置异常通知的步骤 (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. mybatis BigDecimal Double Long 的坑爹事

    写接口的时候别用 public Map<String,Double> selectForRealRemainer(Orders orders); 用这样就行 public Map<S ...

  2. 24 【python入门指南】class

    一.类 1.1,构造函数,析构函数 #!/bin/python class dog(): def __init__(self, age, name): self.age = age self.name ...

  3. c#: 模态窗口最小化主窗口

    起源: 产品中,通常有些耗时操作比如视频转换.DVD刻录等,在模态窗口中执行.此时最小化它,主窗体不能跟着最小化,影响操作体验. 如何让主窗体最小化,并且可以还原呢?搜索一番,未找到满意结果,自己动手 ...

  4. 【Linux 线程】常用线程函数复习《一》

    1.pthread_create以及pthread_self函数 /****************************************************************** ...

  5. python+selenium环境安装

    目前 selenium 版本已经升级到 3.7了,网上的大部分教程是基于 2.x写的,所 以在学习前先要弄清楚版本号,这点非常重要.本系列依然以 selenium2 为基础, 目前 selenium3 ...

  6. powerdesigner中实现PDM到MYSQl数据库的转换

    一.使用PowerDesigner制作建库脚本 1.设计CDM(Conceptual Data Model) 2.选择 Tools -> Generate Physical Data Model ...

  7. WebAPI支持Session

    1.在App_Start/WebApiConfig.cs中建立建立HttpControllerHandler和HttpControllerRouteHandler 并覆写它: public class ...

  8. vue2.0跨域携带cookie和IE兼容

    /*vue-resource为了跨域获取到cookie做的操作*/ 1vue-resource跨域问题Vue.http.interceptors.push(function(request, next ...

  9. Soa思想分布式服务webservice WCF

    什么是分布式事务 分布式事务就是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.以上是百度百科的解释,简单的说,就是一次大的操作由不同的小操作组成,这 ...

  10. js filter关键字

    filter filter也是一个常用的操作,它用于把Array的某些元素过滤掉,然后返回剩下的元素. 和map()类似,Array的filter()也接收一个函数.和map()不同的是,filter ...