AOP的异常通知
一、配置异常通知的步骤 (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的异常通知的更多相关文章
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- Spring 通过来AOP 实现前置,环绕,异常通知,注解(转)
本节主要内容: 1. Spring AOP前置通知案例 2. Spring AOP环绕通知案例 3. Spring AOP异常通知案例 4. Spring AOP注解使 ...
- aop编程之后置通知,环绕通知和异常通知
---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html 具体流程如下: 1. ...
- [转载] Spring框架——AOP前置、后置、环绕、异常通知
通知类型: 步骤: 1. 定义接口 2. 编写对象(被代理对象=目标对象) 3. 编写通知(前置通知目标方法调用前调用) 4. 在beans.xml文件配置 4.1 配置 被代理对象=目标对象 4.2 ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等
实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...
- :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式
三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...
随机推荐
- Mac mysql sql_model引起的问题
问题: 我这里时应为timestamp引起的,服务器的数据使用的mysql5.本地使用的是mysql8,sql_model 不同导致数据不能够在数据库中添加. 解决: 在/etc/下查找my.cnf文 ...
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- jggrid应用,后台c#
参考网址: 1.https://www.cnblogs.com/miro/p/jqGrid.html 2.https://blog.csdn.net/ainuser/article/details/6 ...
- ubuntu下手动安装php-amqp模块教程
用于ubuntu的默认源里面没有php5-amqp这个包,所以要用上amqp得考手动编译. 参考手册 http://php.net/manual/pl/book.amqp.php 首先安装必须的php ...
- stark组件之过滤操作【模仿Django的admin】
一.先看下django的admin是如何实现过滤操作 首先在配置类中顶一个list_filter的列表,把要过滤的字段作为元素写i进去就可以了 class testbook(admin.ModelAd ...
- 联想G510安装win7系统
1.插入系统盘重启(要按Fn+f12) 2默认下一步 3.选择自定义 4.选择需要将系统安装到的分区,格式化,然后下一步 5.默认下一步 安装到这里就成功了 6.问题处理: 第4步时出现 Window ...
- vue 内引入jquery
1. npm i jquery -- save 2. import $ from 'jquery' window.$ = $ window.jQuery = $ export default $ 这 ...
- 手机端适配iPhoneX
iPhoneX取消了物理按键,改成底部小黑条,这一改动导致网页出现比较尴尬的屏幕适配问题.对于网页而言,顶部(刘海部位)的适配问题浏览器已经做了处理,所以我们只需要关注底部与小黑条的适配问题即可(即常 ...
- PAT 1013 数素数 (20)(代码)
1013 数素数 (20)(20 分) 令P~i~表示第i个素数.现任给两个正整数M <= N <= 10^4^,请输出P~M~到P~N~的所有素数. 输入格式: 输入在一行中给出M和N, ...
- Win10传递优化设置技巧
什么是“传递优化缓存” “传递优化”是微软为了加快Windows更新和Microsoft Store应用更新的下载速度,而在Windows10中引入的一种“自组织分布式本地化缓存”设计,可以在用户电脑 ...