一 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. C#中与C++中的 LPWSTR(wchar_t *) 对应的类型

    1.设置 CharSet = CharSet.Unicode [DllImport("test.dll", EntryPoint = "sum()", Char ...

  2. linux下访问window的共享文件,在命令行实现方法

    1.挂载共享目录 mount -t cifs //192.168.0.1/aa  /tmp/export -o username=text,password=test //192.168.0.1/aa ...

  3. 算法篇【递归2 -- N皇后问题】

    问题:输入整数N,要求在N*N的棋盘上,互相不能攻击,不在同一行同一列上,切不在对角线上,输出全部方案. 输入: 4 输出: 2  4  1  3 3  1  4  2 思路: 假设在前k-1个摆好的 ...

  4. A class of finite groups with abelian 2-Sylow subgroups By CHIH-HAN SAH

    Remark: 1.An element of a group which conjugate to its inverse is called a real element. If $G$ has ...

  5. Appium+python自动化1-环境搭建

    一.前言 appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.小编擅长Python,所以接下来的教程是appium+ ...

  6. laravel中的模型关联之(一对多)

    一对多 一对多就相当于,一个用户有多篇文章,这多篇文章都对应一个用户 这是一张文章表,一个用户有多篇文章,这里是在用户模型里面获取用户的所有文章, 第二个参数就是获取的模型文章表(post)里面的用户 ...

  7. 模拟点击事件在alert前不起作用

    本来想在ajax提交前点击一下模态框,直到返回处理之前都显示正在保存,发现如标题的现象,几经折腾没找到解决办法,发现可能是 alert线程阻塞(冒泡)引起的,也没找到解决办法,于是借助第三方插件lay ...

  8. Java_13.1.1 字符串的应用

    1获取一个字符串中,另一个字符串出现的次数 思想:      1. indexOf到字符串中到第一次出现的索引      2. 找到的索引+被找字符串长度,截取字符串       3. 计数器++ p ...

  9. export命令

    http://blog.csdn.net/wl_fln/article/details/7258294 http://man.linuxde.net/export export命令 功能说明:设置或显 ...

  10. 【gRPC使用问题2】按照问题1操作生成出来的代码,import的proto内定义的message未生成出来

    1.问题 其实元数据proto里是有定义message,但是 这个message的定义是在另一个 proto文件内,被 api.proto导入,事实上 我是对 api.proto 进行命令行生成代码的 ...