一、配置异常通知的步骤 (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. as3.0复制影片简介(自我复制的三种形式)

    //mc是被复制影片简介的实例名,(===在库中找到mc影片简介,右击“属性”,点击“为actionscript导出”,选中确定即可===这个是重点) var newSprite:Sprite=mc; ...

  2. Django之crm

    crm注册 crm注册Form from django import forms from crm import models from django.core.exceptions import V ...

  3. 高德地图开发者平台获取sHA1值

    一般在  Application 中进行初始化 /** * 获取高德SHA1值 * */ public static String sHA1(Context context) { try { Pack ...

  4. 5. Longest Palindromic Substring (DP)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. spring mybatis springmvc整合

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  6. 第一个springMVC小程序

    1首先配置一个前端控制器,在web.xml文件中配置(dispatcherservlet) <!-- 前端控制器配置 --> <servlet> <servlet-nam ...

  7. 博客(二)注册页面django

    首先上一个html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. 用webstorm开发前端项目前的一些配置

    每日的开发都在紧张中进行,代码的运用和流通性大,有些想酝酿品尝的东西,来不及停留而留下遗憾,基于此,想起几次前辈们的建议,用写博客来记录这些曾在我们内心荡漾的一些东西. 需要安装的软件:WebStor ...

  9. swift -2018 - 创建PCH文件

    第一种:简单方式 在swift文件 创建一个OC文件 1> command + n 2> 3> 此时 你会发现有提示 让你创建桥接文件 4>删除 OC文件 5> 直接复制 ...

  10. 11-web网页制作APP

    如何将H5和WebApp 加壳成apk.ipa     问题:已经做好的纯H5的站点 想分别加两个壳子,变成apk和ipa ,要怎么实现? 要点: 1. app只是壳子,打开app直接跳转到H5的Ur ...