一 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. MyEclipse2014安装aptana插件

    1. 2. aptana插件下载地址 链接: https://pan.baidu.com/s/1sloiAK1 密码: a1nh 3. 4. 确认是否安装成功

  2. SQL%ROWCOUNT作用

    SQL%ROWCOUNT是一个游标属性,而SQL中的DML操作实际上是一种隐式的游标操作,在做insert,update,delete,merge以及select into操作时,Oracle会打开一 ...

  3. zabbix 短信报警

    使用的短信平台是云片网,接口请看官网短信接口API文档,有示例 进入server服务器存放脚本的文件夹,默认路径是 [root@test zabbix]# cat zabbix_server.conf ...

  4. Android 实现简单 倒计时60秒,一次1秒

    倒计时功能如上图所示,其实就几行代码即可实现效果啦!!! /** 倒计时60秒,一次1秒 */ CountDownTimer timer = new CountDownTimer(60*1000, 1 ...

  5. Watcher、ZK状态、事件类型 ,权限

    zookeeper有watch事件,是一次性触发的,当watch监视的数据发生变化时,通知设置了该watch的client,即watcher. 同样,其watcher是监听数据发送了某些变化,那就一定 ...

  6. 33 【kebernetes】一个错误的解决方案

    在安装或者重新安装kubernetes时,我碰到了这个错误: Unable to update cni config: No networks found in /etc/cni/net.d/ 这个错 ...

  7. [剑指Offer]25-合并两个排序链表

    题目链接 https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&tqId=11169&t ...

  8. openal支持的通道数和声道数

    alext.h:  #define AL_FORMAT_QUAD8 0x1204 101 #define AL_FORMAT_QUAD16 0x1205 102 #define AL_FORMAT_Q ...

  9. f5 irules

    1.插入XFF when HTTP_REQUEST { if { [HTTP::header exists X-Forward-For] } { set old_xff [HTTP::header v ...

  10. django的中间件:process_request|process_response|process_view|process_exception

    MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware. ...