用spring的AOP实现异常拦截
    Spring支持四种拦截类型:目标方法调用前(before),目标方法调用后(after),目标方法调用前后(around),以及目标方法抛出异常(throw)。
    最近用到spring的AOP来实现异常拦截,用到了spring的ThrowsAdvice。ThrowsAdvice是一个标示接口,我们可以在类中定义一个或多个,来捕获定义异常通知的bean抛出的异常,并在抛出异常前执行相应的方法。

    1. import org.springframework.aop.ThrowsAdvice;
    2. import org.springframework.aop.framework.ProxyFactory;
    3. import java.lang.reflect.Method;
    4. /**
    5. * Copyright 2007 GuangZhou Cotel Co. Ltd.
    6. * All right reserved.
    7. * 异常拦截类.
    8. * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
    9. * @version 1.0
    10. * Creation date: 2007-7-24 - 下午08:12:25
    11. */
    12. public class ExceptionAdvisor implements ThrowsAdvice {
    13. public static void main(String[] args) {
    14. TestBean bean = new TestBean();
    15. ProxyFactory pf = new ProxyFactory();
    16. pf.setTarget(bean);
    17. pf.addAdvice(new ExceptionAdvisor());
    18. TestBean proxy = (TestBean) pf.getProxy();
    19. try {
    20. proxy.method1();
    21. } catch (Exception ignore) {
    22. }
    23. try {
    24. proxy.changeToNumber("amigo");
    25. } catch (Exception ignore) {
    26. }
    27. }
    28. /**
    29. * 对未知异常的处理.
    30. * @param method
    31. * @param args
    32. * @param target
    33. * @param ex
    34. * @throws Throwable
    35. * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
    36. * Creation date: 2007-7-24 - 下午03:35:02
    37. */
    38. public void afterThrowing(Method method, Object[] args, Object target,
    39. Exception ex) throws Throwable {
    40. System.out.println("*************************************");
    41. System.out.println("Error happened in class: " + target.getClass().getName());
    42. System.out.println("Error happened in method: " + method.getName());
    43. for (int i = 0; i < args.length; i++) {
    44. System.out.println("arg[" + i + "]: " + args[i]);
    45. }
    46. System.out.println("Exception class: " + ex.getClass().getName());
    47. System.out.println("*************************************");
    48. }
    49. /**
    50. * 对NullPointerException异常的处理
    51. * @param method
    52. * @param args
    53. * @param target
    54. * @param ex
    55. * @throws Throwable
    56. * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
    57. * Creation date: 2007-7-24 - 下午01:17:35
    58. */
    59. public void afterThrowing(Method method, Object[] args, Object target,
    60. NullPointerException ex) throws Throwable {
    61. System.out.println("*************************************");
    62. System.out.println("Error happened in class: " + target.getClass().getName());
    63. System.out.println("Error happened in method: " + method.getName());
    64. for (int i = 0; i < args.length; i++) {
    65. System.out.println("args[" + i + "]: " + args[i]);
    66. }
    67. System.out.println("Exception class: " + ex.getClass().getName());
    68. System.out.println("*************************************");
    69. }
    70. }

    用spring boot的ControllerAdvice 只能是拦截controller类

    http://www.blogjava.net/amigoxie/archive/2007/07/24/132142.html