通常我们对于异常的处理方式都是大同小异的,要么直接捕获并处理,要么让它抛向上一层,要么就是记录到日志里,或者发邮件提供管理员,但这样下来一个项目中便会到处充斥着 try/catch ,并且 catch 中的代码基本类似,于是我们闻到的其中难闻的坏味道。

本文将介绍如何通过 Spring.AOP 特性实现异常的统一处理,如果我们需要在异常发生时做一些操作的话我们就必须实现 Spring.Aop.IThrowsAdvice,该接口没有任何实现方法,是一个空接口,它仅仅做为一个标记接口而存在,但实现了 IThrowsAdvice 接口的类必须定义至少一个 AfterThrowing 方法,方法的签名如下:AfterThrowing([MethodInfo method, Object[] args, Object target], Exception subclass);
其中中括号括起来的前三个参数是可选的,返回值可以是任意数据类型。 Spring.Aop.Framework.Adapter.ThrowsAdviceInterceptor 类实现对实现了 Spring.Aop.IThrowsAdvice 派生类中的方法依赖注入,其中的 ThrowsAdviceInterceptor() 方法检查 Spring.Aop.IThrowsAdvice 的派生类是否定义了至少一个异常处理方法,如果没有则抛出 ArgumentException 异常,MapAllExceptionHandlingMethods()方法则在定义好的重载方法中查找出异常类型与最后一个参数所定义的类型中最接近的方法,而且我们不应该在其中实现了两个相同异常类型的方法,即使他们的参数数目不同,否则也将抛出 ArgumentException 异常。
[下面引用自《Spring 技术手册》第4章 P94 页中的一段话]注意到当异常发生时, Throw Advice 的任务只是执行对应的方法,您并不能在 Throw Advice 中将异常处理掉,在 Throw Advice 执行完毕后,原告的异常仍将传播至应用程序之中, Throw Advice 并不介入应用程序的异常处理,异常处理仍旧是应用程序本身所要负责的,如果想要在 Throw Advice 处理时中止应用程序的处理流程,作法是抛出其它的异常。
接下来看个 Throws Advice 的实际例子,首先定义 IHello 接口:
- using System;  
 -     namespace TestThrowAdvice  
 -     {  
 -         public interface IHello  
 -         {  
 -             void Hello(string name);  
 -         }  
 -     } 
 
接着定义一个 HelloSpeaker 类来实现 IHello 接口,并在 Hello() 方法中模拟程序发生错误时的异常抛出:
- using System;  
 -     namespace TestThrowAdvice  
 -     {  
 -         public class HelloSpeaker : IHello  
 -         {  
 -             public void Hello(string name)  
 -             {  
 -                 Console.WriteLine("Hello, " + name);  
 -                 //抱歉! 程序错误! 发生异常 XD  
 -                 throw new Exception("发生异常");  
 -             }  
 -         }  
 -     } 
 
如果您需要在应用程序抛出异常时,介入 Throw Advice 提供一些服务,例如记录一些异常信息,则可以实现 Spring.Aop.IThrowsAdvice 接口,在这个例子中我使用了 log4net 组件来实现日志的记录:
- using System;  
 - using Spring.Aop;  
 - using log4net;  
 - using log4net.Core;  
 - using System.Reflection;  
 - [assembly: log4net.Config.XmlConfigurator(Watch = true)]  
 - namespace TestThrowAdvice  
 - {  
 -    public class SomeThrowAdvice : IThrowsAdvice  
 -   {  
 -       private ILog logger;  
 -       public SomeThrowAdvice()  
 -       {  
 -          logger = LogManager.GetLogger(this.GetType());  
 -       }  
 -       public void AfterThrowing(MethodInfo method, Object[] args, Object target, Exception exception)  
 -       {  
 -          // 记录异常  
 -         logger.Info("记录异常", exception);  
 -       }  
 -    }  
 -  } 
 
接着在配置文件(我这里使用了独立配置文件)中写下以下的定义,让 Throw Advice 在异常发生时提供记录服务:
-  xml version="1.0" encoding="utf-8"?> 
 -      xmlns="http://www.Springframework.net" xmlns:xsi=                                      "http://www.w3.org/2001/XMLSchema-instance" 
 -              xsi:schemaLocation="http://www.Springframework.net  
 -              http://www.Springframework.net/xsd/Spring-objects.xsd"> 
 -       
 -       
 -       
 -          name="ProxyInterfaces"> 
 -           
  -             TestThrowAdvice.IHello,TestThrowAdvice value> 
 -            list> 
 -          property> 
 -          name="Target"> 
 -           
[ object="HelloSpeaker" /> ] -          property> 
 -          name="InterceptorNames"> 
 -           
  -             SomeThrowAdvice value> 
 -            list> 
 -          property> 
 -        object> 
 -      objects> 
 
最后剩下我们的程序入口 Main() 函数了:
- using System;  
 - using Spring.Context;  
 - using Spring.Context.Support;  
 -     namespace TestThrowAdvice  
 -     {  
 -         public class Program  
 -         {  
 -             static void Main(string[] args)  
 -             {  
 -                 log4net.Config.XmlConfigurator.Configure();  
 -                 IApplicationContext context = new XmlApplicationContext(@"../../SpringNet.xml");  
 -                 IHello helloProxy = (IHello)context.GetObject("HelloProxy");  
 -                 try  
 -                 {  
 -                     helloProxy.Hello("Justin");  
 -                 }  
 -                 catch (Exception ex)  
 -                 {  
 -                     // 应用程序的异常处理  
 -                     Console.WriteLine(ex.Message);  
 -                 }  
 -             }  
 -         }  
 -     } 
 
【编辑推荐】
- Spring控制反转(IoC)容器
 - Spring is coming
 - 在Spring中进行集成测试
 - Spring中的TopLink ServerSession
 - Spring声明式事务
 
            
            网页题目:Spring AOP学习笔记            
            标题来源:
http://www.ptruijie.cn/article/codehgc.html