一 schema-based异常通知

第一步:创建通知类 :新建一个类实现 throwsAdvice 接口,throwsAdvice接口只是标记接口里面并没有任何方法,必须自己写方法,且必须叫 afterThrowing,afterThrowing方法里面的参数,有两种参数方式, 必须是 1 个或 4 个

注意:参数的异常类型要,切点报的异常类型一致,  已经jar包要和jdk版本兼容(出问题解决了好长事件),因为有时候切点抛出的异常会有多种类型,为了正确接受,建议在异常通知类里面,afterThrowing的参数写成Exception,这样可以保证兼容

package com.airplan.pojo;

import org.springframework.aop.ThrowsAdvice;

public class ExceptionAdvice implements ThrowsAdvice{
public void afterThrowing(Exception ex) throws Throwable {
System.out.println(ex.getMessage());
System.out.println("异常通知执行"); }
}

第二种写法:

package com.airplan.pojo;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class ExceptionAdvice implements ThrowsAdvice{

    public void afterThrowing(Method m, Object[] args,Object target, Exception ex) {
System.out.println("执行异常通知"); }
}

第二步配置切入点,切面,通知所在的类

<?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="afterAdv" class="com.airplan.pojo.MyAfterAdvice"></bean>
<!-- 配置通知所在的类,配置通知 -->
<bean id="exceptionAdvice" class="com.airplan.pojo.ExceptionAdvice"> </bean> <!-- 配置切面 -->
<aop:config>
<!--
配置切点,特别注意expression的写法
-->
<aop:pointcut expression="execution(* com.airplan.pojo.PointCutClass.testFun(..))" id="test"/>
<aop:advisor advice-ref="exceptionAdvice" pointcut-ref="test"/>
</aop:config> <!-- 配置切点所在的类 -->
<bean id="pointCutClass" class="com.airplan.pojo.PointCutClass"></bean> </beans>

测试代码;

package com.airplan.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.PointCutClass; public class AopDemo {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PointCutClass pointCutClass = ac.getBean("pointCutClass",PointCutClass.class);
pointCutClass.testFun();
//pointCutClass.throwPointCut();
}
}

二 schema-based 环绕通知

第一步:创建环绕通知类

package com.airplan.pojo;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class AroundAdv implements MethodInterceptor{ @Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("环绕-前置");
Object result = arg0.proceed();//放行,调用切点方法
System.out.println("环绕-后置");
return result; } }

第二步配置  

<bean id="aroundAdv" class="com.airplan.pojo.AroundAdv"></bean>
<!-- 配置切面 -->
<aop:config>
<!--
配置切点,特别注意expression的写法
-->
<aop:pointcut expression="execution(* com.airplan.pojo.PointCutClass.throwPointCut(..))" id="test"/>
<aop:advisor advice-ref="aroundAdv" pointcut-ref="test"/>
</aop:config> <!-- 配置切点所在的类 -->
<bean id="pointCutClass" class="com.airplan.pojo.PointCutClass"></bean>

第三步测试

package com.airplan.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.PointCutClass; public class AopDemo {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PointCutClass pointCutClass = ac.getBean("pointCutClass",PointCutClass.class);
pointCutClass.throwPointCut();
//pointCutClass.throwPointCut();
}
}

spring学习 十 schema-based 异常通知,和环绕通知的更多相关文章

  1. Spring AOP--返回通知,异常通知和环绕通知

    在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...

  2. Spring学习十四----------Spring AOP实例

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

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

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

  4. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  5. spring学习 十 schema-based 前置后后置通知

    spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在<a ...

  6. spring学习 十四 注解AOP 通知传递参数

    我们在对切点进行增强时,不建议对切点进行任何修改,因此不加以使用@PointCut注解打在切点上,尽量只在Advice上打注解(Before,After等),如果要在通知中接受切点的参数,可以使用Jo ...

  7. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  8. spring学习十九 常用注解

    1. @Component 创建类对象,相当于配置<bean/>2. @Service 与@Component 功能相同. 2.1 写在 ServiceImpl 类上.3. @Reposi ...

  9. Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)

    在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...

随机推荐

  1. 常用的jquerymobil 站点

    http://www.jqmapi.com/api1.2/ Jquery Mobile 中文API站 https://codiqa.com/demo   jquerymobil UI编辑器 https ...

  2. 新版本PHP使用更方便了

    $hostname_conn = "";$database_conn = "";$username_conn = "root";$passw ...

  3. java面试题:Spring

    Spring 面试时,最好能结合底层代码说出IOC,AOP或Spring MVC的流程,能说出拦截器的底层. 如果看过Spring的源码,并能结合设计模式表达,是很大的加分项. IOC Q:讲一下IO ...

  4. helm 更改为国内源

     helm init --upgrade -i slpcat/tiller:v2.8.2 --stable-repo-url https://kubernetes.oss-cn-hangzhou.al ...

  5. perl-我的第一个程序

    1.问题描述: 总共90位长度的位流数据,其中只有5位的数据为1,其余位全部为0.统计好多组5位的简化数据(每一位之间空格隔开,每一组一行),将其扩展到90位. #!D:/EDA/Perl/bin $ ...

  6. HDU-1042.N!(大数与小数相乘的乘法模拟)

    本题大意:给定一个10000以内的整数n,让你求出n!并输出. 本题思路:先初始化一个存放答案的数组ans,初始ans[0] = 1,并初始化其剩下的元素为0,接着就从2开始依次与ans数组内的每一个 ...

  7. Unity 场景分页插件 World Streamer 支持无限大地图的解决方案(二)

    Terrain Streaming 可以用WorldCreator创建Tile地形,然后用WorldStreamer实现分块地图.比如10000*10000(16平方公里) 的地形,需要1000*10 ...

  8. stm32学习基本知识点

    1.AHB系统总线分为APB1(36MHz)和APB2(72MHz),其中2>1,意思是APB2接高速设备 2.Stm32f10x.h相当于reg52.h(里面有基本的位操作定义),另一个为st ...

  9. ubuntu下安装、破解securtCRT工具

    官网:https://www.vandyke.com/ 下载好的安装包以及破解工具的网盘地址:链接: https://pan.baidu.com/s/1UEKEy-aob7WdfPLR4PNJmg 提 ...

  10. 将unitest整合和python发送测试报告

    废话少说先上代码 # -*- coding:UTF-8 -*- __autor__ = 'zhouli' __date__ = '2018/11/12 21:29' import unittest i ...