统一日志和异常管理配置好后,SSH项目中,代码以往散落的log.info() 和 try..catch..finally 再也不见踪影!

统一日志异常实现类:

[java] view plaincopy
  1. package com.pilelot.web.util;
  2. import org.apache.log4j.Logger;
  3. import org.springframework.aop.ThrowsAdvice;
  4. import org.springframework.dao.DataAccessException;
  5. import java.io.IOException;
  6. import java.lang.reflect.Method;
  7. import java.sql.SQLException;
  8. /**
  9. * 由Spring AOP调用 输出异常信息,把程序异常抛向业务异常
  10. *
  11. * @author Andy Chan
  12. *
  13. */
  14. public class ExceptionAdvisor implements ThrowsAdvice
  15. {
  16. public void afterThrowing(Method method, Object[] args, Object target,
  17. Exception ex) throws Throwable
  18. {
  19. // 在后台中输出错误异常异常信息,通过log4j输出。
  20. Logger log = Logger.getLogger(target.getClass());
  21. log.info("**************************************************************");
  22. log.info("Error happened in class: " + target.getClass().getName());
  23. log.info("Error happened in method: " + method.getName());
  24. for (int i = 0; i < args.length; i++)
  25. {
  26. log.info("arg[" + i + "]: " + args[i]);
  27. }
  28. log.info("Exception class: " + ex.getClass().getName());
  29. log.info("ex.getMessage():" + ex.getMessage());
  30. ex.printStackTrace();
  31. log.info("**************************************************************");
  32. // 在这里判断异常,根据不同的异常返回错误。
  33. if (ex.getClass().equals(DataAccessException.class))
  34. {
  35. ex.printStackTrace();
  36. throw new BusinessException("数据库操作失败!");
  37. } else if (ex.getClass().toString().equals(
  38. NullPointerException.class.toString()))
  39. {
  40. ex.printStackTrace();
  41. throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");
  42. } else if (ex.getClass().equals(IOException.class))
  43. {
  44. ex.printStackTrace();
  45. throw new BusinessException("IO异常!");
  46. } else if (ex.getClass().equals(ClassNotFoundException.class))
  47. {
  48. ex.printStackTrace();
  49. throw new BusinessException("指定的类不存在!");
  50. } else if (ex.getClass().equals(ArithmeticException.class))
  51. {
  52. ex.printStackTrace();
  53. throw new BusinessException("数学运算异常!");
  54. } else if (ex.getClass().equals(ArrayIndexOutOfBoundsException.class))
  55. {
  56. ex.printStackTrace();
  57. throw new BusinessException("数组下标越界!");
  58. } else if (ex.getClass().equals(IllegalArgumentException.class))
  59. {
  60. ex.printStackTrace();
  61. throw new BusinessException("方法的参数错误!");
  62. } else if (ex.getClass().equals(ClassCastException.class))
  63. {
  64. ex.printStackTrace();
  65. throw new BusinessException("类型强制转换错误!");
  66. } else if (ex.getClass().equals(SecurityException.class))
  67. {
  68. ex.printStackTrace();
  69. throw new BusinessException("违背安全原则异常!");
  70. } else if (ex.getClass().equals(SQLException.class))
  71. {
  72. ex.printStackTrace();
  73. throw new BusinessException("操作数据库异常!");
  74. } else if (ex.getClass().equals(NoSuchMethodError.class))
  75. {
  76. ex.printStackTrace();
  77. throw new BusinessException("方法末找到异常!");
  78. } else if (ex.getClass().equals(InternalError.class))
  79. {
  80. ex.printStackTrace();
  81. throw new BusinessException("Java虚拟机发生了内部错误");
  82. } else
  83. {
  84. ex.printStackTrace();
  85. throw new BusinessException("程序内部错误,操作失败!" + ex.getMessage());
  86. }
  87. }
  88. }

自定义业务异常处理类 友好提示:

[java] view plaincopy
  1. package com.pilelot.web.util;
  2. /**
  3. * 自定义业务异常处理类    友好提示
  4. * @author Andy Chan
  5. *
  6. */
  7. public class BusinessException extends RuntimeException
  8. {
  9. private static final long serialVersionUID = 3152616724785436891L;
  10. public BusinessException(String frdMessage)
  11. {
  12. super(createFriendlyErrMsg(frdMessage));
  13. }
  14. public BusinessException(Throwable throwable)
  15. {
  16. super(throwable);
  17. }
  18. public BusinessException(Throwable throwable, String frdMessage)
  19. {
  20. super(throwable);
  21. }
  22. private static String createFriendlyErrMsg(String msgBody)
  23. {
  24. String prefixStr = "抱歉,";
  25. String suffixStr = " 请稍后再试或与管理员联系!";
  26. StringBuffer friendlyErrMsg = new StringBuffer("");
  27. friendlyErrMsg.append(prefixStr);
  28. friendlyErrMsg.append(msgBody);
  29. friendlyErrMsg.append(suffixStr);
  30. return friendlyErrMsg.toString();
  31. }
  32. }

统一日志处理实现类:

[java] view plaincopy
  1. package com.pilelot.web.util;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.aopalliance.intercept.MethodInvocation;
  4. import org.apache.log4j.Logger;
  5. /**
  6. * Spring 统一日志处理实现类
  7. * @author Andy Chan
  8. *
  9. */
  10. public class LogInterceptor implements MethodInterceptor
  11. {
  12. public Object invoke(MethodInvocation invocation) throws Throwable
  13. {
  14. Logger loger = Logger.getLogger(invocation.getClass());
  15. loger.info("--Log By Andy Chan -----------------------------------------------------------------------------");
  16. loger.info(invocation.getMethod() + ":BEGIN!--(Andy ChanLOG)");// 方法前的操作
  17. Object obj = invocation.proceed();// 执行需要Log的方法
  18. loger.info(invocation.getMethod() + ":END!--(Andy ChanLOG)");// 方法后的操作
  19. loger.info("-------------------------------------------------------------------------------------------------");
  20. return obj;
  21. }
  22. }

Spring配置文件添加:

[html] view plaincopy
  1. <!-- Spring 统一日志处理   LogInterceptor拦截器 配置 -->
  2. <bean id="logLnterceptor" class="com.pilelot.web.util.LogInterceptor"/>
  3. <!-- Spring 统一异常处理  ExceptionAdvisor配置 -->
  4. <bean id="exceptionHandler" class="com.pilelot.web.util.ExceptionAdvisor"></bean>
  5. <!-- Bean自动代理处理器 配置-->
  6. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
  7. <property name="beanNames">
  8. <list>    <!-- 配置需要进行日志记录的Service和Dao -->
  9. <value>commonDao</value>
  10. <!-- 配置所有Service结尾命名的Bean,即所有Service层的类都要经过exceptionHandler异常处理类 -->
  11. <value>*Service</value>  <!-- Service层的Bean ID 命名要以Service结尾 -->
  12. </list>
  13. </property>
  14. <property name="interceptorNames">
  15. <list>
  16. <value>exceptionHandler</value>
  17. <value>logLnterceptor</value>
  18. <!--<value>transactionInterceptor</value>-->
  19. </list>
  20. </property>
  21. </bean>
  22. lt;!-- ——————————————————Spring 统一日志处理 + 统一异常处理  配置结束—————————————悲伤的分隔线—————————— -->

这样简单三步,就实现了由Spring统一日志+统一异常管理,代码清爽了不少!

使用Spring进行统一日志管理 + 统一异常管理相关推荐

  1. Spring MVC 中使用AOP 进行统一日志管理--XML配置实现

    1.介绍 上一篇博客写了使用AOP进行统一日志管理的注解版实现,今天写一下使用XML配置实现版本,与上篇不同的是上次我们记录的Controller层日志,这次我们记录的是Service层的日志.使用的 ...

  2. spring aop统一进行日常及异常的处理

    什么是AOP 以下摘自百度百科: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.A ...

  3. Spring Boot AOP 扫盲,实现接口访问的统一日志记录

    AOP 是 Spring 体系中非常重要的两个概念之一(另外一个是 IoC),今天这篇文章就来带大家通过实战的方式,在编程猫 SpringBoot 项目中使用 AOP 技术为 controller 层 ...

  4. Java项目构建基础:统一结果,统一异常,统一日志(好文推荐)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:永动的图灵机 juejin.im/post/5e073980 ...

  5. 十、springboot注解式AOP(@Aspect)统一日志管理

    springboot注解式AOP(@Aspect)统一日志管理 简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...

  6. Java项目构建基础:统一结果,统一异常,统一日志

    统一结果返回 目前的前后端开发大部分数据的传输格式都是json,因此定义一个统一规范的数据格式有利于前后端的交互与UI的展示. 统一结果的一般形式 是否响应成功: 响应状态码: 状态码描述: 响应数据 ...

  7. delphi自定义统一基础设置_Java项目构建基础:统一结果,统一异常,统一日志...

    作者:永动的图灵机 juejin.im/post/5e073980f265da33f8653f2e 统一结果返回 目前的前后端开发大部分数据的传输格式都是json,因此定义一个统一规范的数据格式有利于 ...

  8. java 统一日志_基于log4j实现统一日志管理

    背景: 一般操作系统级的告警有相关的软件,但我们应用级日志往往无法统一监控.分析.因为最近的项目是比较大的一个平台,有七.八个子系统,weblogic域也有三.四个.如果用户自身能够实时监控到应用级致 ...

  9. Spring boot统一日志记录

    统一日志记录 开发的时候 用到都是一个框架 很多的框架(slf4j+logback): Spring(commons-logging) Hibernate(jboss-logging) MyBatis ...

最新文章

  1. 关系管理系统:js代码生成select的出生日期
  2. ssh远程操作服务器
  3. Chart.js-线形图分析(参数分析+例图)
  4. Python学习之使用Python生成PDF报告
  5. linux命令dmesg查看进程被杀死原因
  6. Algorithms_基础数据结构(02)_线性表之链表_单向链表
  7. 本地缓存需要高时效性怎么办_详解微信小程序缓存--缓存时效性
  8. 全国二级计算机理论知识,2021年度全国计算机等级考试二级MSOffice常考知识点基础知识部分.doc...
  9. Linux 命令之 arch --显示主机的硬件结构类型
  10. 林子大了,什么鸟都有----.NET运用String的十八层境界
  11. __attribute__ 之weak,alias属性
  12. PyTorch 1.0 中文文档:torch.hub
  13. C++根据.h文件批量生成需要的函数框架
  14. puppeteer中如何复用启动中的浏览器
  15. Bootstrap 警告
  16. [从头学数学] 第193节 推理与证明
  17. Tensorflow实现进阶的神经网络
  18. 通过JAVA从高德地图URL连接获取json数据 解析并存入数据库的程序举例
  19. uniapp插件市场-涂图视频编辑-美妆-剪辑-微整形原生sdk插件发布-优雅草科技
  20. MySQL学习笔记--常用存储引擎InnoDB与MyISAM总结

热门文章

  1. OpenStack如何实现高可用集群介绍
  2. 如何设置 Linux 上 SSH 登录的 Email 提醒
  3. QT开发(五十)——QT串口编程基础
  4. 职责链模式里面必须要知道的事情
  5. puppet aix之自动化用户管理
  6. 构建RHEL上的extmail
  7. 小程序 reduce_使用reduce制作的10个更多实用程序功能
  8. 老年痴呆 数字化_设计老年人愉快数字体验的5条原则
  9. 2018湖湘杯海选复赛Writeup
  10. 脱离公式谈谈对反向传播算法的理解