一 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. NumPy 统计函数

    NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下: numpy.amin() 和 numpy.amax() numpy.a ...

  2. Lua面试题目

    1.Lua的特性 轻量级: 它用标准C语言编写并以源代码形式开放,编译后仅仅一百余K,可以很方便的嵌入别的程序里. 可扩展: Lua提供了非常易于使用的扩展接口和机制:由宿主语言(通常是C或C++)提 ...

  3. Unity5权威讲解+项目源码+MP4

    扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主

  4. ES5之函数的间接调用 ( call、apply )、绑定 ( bind )

    call().apply()的第一个实参是函数调用的上下文,在函数体内通过this来获得对它的引用. call()将实参用逗号分隔:apply ()将实参放入数组.类数组对象中. function h ...

  5. vue 返回上一页后,上一页由参数渲染的内容无法显示

    思路1:将参数传递给第二个页面后,返回上一页时,再讲参数传回第一页(此方法适用于层级少的)(亲测有效) 思路2:将参数放到全局变量中(还未尝试过)

  6. Mycat性能调优指南

    本篇内容来自于网络 JVM调优: 内存占用分两部分:java堆内存+直接内存映射(DirectBuffer占用),建议堆内存 适度大小,直接映射内存尽可能大,两种一起占据操作系统的1/2-2/3的内存 ...

  7. call指令

    CPU执行call指令时,进行两步操作: 将当前的IP或CS和IP压入栈中; 转移. call指令不能实现短转移,除此之外,call指令实现转移的方法和jmp指令的原理相同. 1)依据位移进行转移的c ...

  8. iOS | 使用HBuilder进行云端打包步骤

    1.先在HBuilder本地项目中的Manifest.json文件中进行项目配置,将应用的图标和启动图,按照固定的尺寸进行配置.设置应用名称,版本号, 这里的appid不需要修改,是HBuilder自 ...

  9. Jmeter常用脚本开发之JDBC请求

    简单说明:JDBC请求就是使用Jmeter连接数据库,执行sql语句,并返回对应的响应结果 步骤: 1.引入使用的数据库的驱动jar包,使用不同的数据库,我们需要引入不同的jar包.本文使用的MySQ ...

  10. poj 3624 && hdu 2955(背包入门)

    http://poj.org/problem?id=3624 背包中最基础的01背包,大意是有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使价值总 ...