异常通知(Schema-based方式)

异常通知(Schema-based方式)

profile-avatar

又双叒叕是程序汪2020-12-27 20:47

异常通知(Schema-based方式)

Spring中有12种配置方法,其中我们掌握4种就差不多了

 

当我们用到Schema-based方式实现异常通知

 

创建一个时间异常通知的类实现ThrowsAdvice,发现并没有给我们提供方法

 

这个时候我们就需要到spring的帮助文档中进行查找

 

Throws advice
Throws advice is invoked after the return of the join point if the join point threw an exception. Spring offers typed throws advice. Note that this means that the org.springframework.aop.ThrowsAdvice interface does not contain any methods: It is a tag interface identifying that the given object implements one or more typed throws advice methods. These should be in the form of:
afterThrowing([Method, args, target], subclassOfThrowable)
Only the last argument is required. The method signatures may have either one or four arguments, depending on whether the advice method is interested in the method and arguments. The following classes are examples of throws advice.
The advice below is invoked if a RemoteException is thrown (including subclasses):
public class RemoteThrowsAdvice implements ThrowsAdvice { 
  public void afterThrowing(RemoteException ex) throws Throwable { 
    // Do something with remote exception 
  }}

 

我们只需要将这个方法复制过去,

 public void afterThrowing(RemoteException ex) throws Throwable { // Do something with remote exception }

 

schema-based配置简单实现方法麻烦

 

测试

 

结果

 

发现并没有实现异常方法

查看帮助文档发现有很多实现方法:

 

换一种实现

public void afterThrowing(Method m, Object[] args, Object target, ServletException ex)
{ // Do something with all arguments
}

 

效果

 

原因:将ServletException改Exception,或者将RemoteException改Exception

 

结果:不是对应的异常类型,还真不能解释出来所以可以用权限最高的异常信息处理

 

当两种处理异常的方法同时存在时

 

Spring的处理方式是倒序的方式,代码从下往上找


 


总结:

1.新建一个类,实现一个ThrowsAdvice的接口

必须写自己的方法,且必须叫做afterThrowing

有两种参数方式,必须是一个或4个

异常类型与切点报的异常类型一致

2.在applicationcontext.xml配置.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值