使用Spring进行统一日志管理 + 统一异常管理
http://blog.csdn.net/king87130/article/details/8011843原文地址 统一日志异常实现类: 1 package com.pilelot.web.util; import org.apache.log4j.Logger;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.dao.DataAccessException; import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException; /**
* 由Spring AOP调用 输出异常信息,把程序异常抛向业务异常
*
* @author Andy Chan
*
*/
public class ExceptionAdvisor implements ThrowsAdvice
{
public void afterThrowing(Method method, Object[] args, Object target,
Exception ex) throws Throwable
{
// 在后台中输出错误异常异常信息,通过log4j输出。
Logger log = Logger.getLogger(target.getClass());
log.info("**************************************************************");
log.info("Error happened in class: " + target.getClass().getName());
log.info("Error happened in method: " + method.getName());
for (int i = 0; i < args.length; i++)
{
log.info("arg[" + i + "]: " + args[i]);
}
log.info("Exception class: " + ex.getClass().getName());
log.info("ex.getMessage():" + ex.getMessage());
ex.printStackTrace();
log.info("**************************************************************"); // 在这里判断异常,根据不同的异常返回错误。
if (ex.getClass().equals(DataAccessException.class))
{
ex.printStackTrace();
throw new BusinessException("数据库操作失败!");
} else if (ex.getClass().toString().equals(
NullPointerException.class.toString()))
{
ex.printStackTrace();
throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");
} else if (ex.getClass().equals(IOException.class))
{
ex.printStackTrace();
throw new BusinessException("IO异常!");
} else if (ex.getClass().equals(ClassNotFoundException.class))
{
ex.printStackTrace();
throw new BusinessException("指定的类不存在!");
} else if (ex.getClass().equals(ArithmeticException.class))
{
ex.printStackTrace();
throw new BusinessException("数学运算异常!");
} else if (ex.getClass().equals(ArrayIndexOutOfBoundsException.class))
{
ex.printStackTrace();
throw new BusinessException("数组下标越界!");
} else if (ex.getClass().equals(IllegalArgumentException.class))
{
ex.printStackTrace();
throw new BusinessException("方法的参数错误!");
} else if (ex.getClass().equals(ClassCastException.class))
{
ex.printStackTrace();
throw new BusinessException("类型强制转换错误!");
} else if (ex.getClass().equals(SecurityException.class))
{
ex.printStackTrace();
throw new BusinessException("违背安全原则异常!");
} else if (ex.getClass().equals(SQLException.class))
{
ex.printStackTrace();
throw new BusinessException("操作数据库异常!");
} else if (ex.getClass().equals(NoSuchMethodError.class))
{
ex.printStackTrace();
throw new BusinessException("方法末找到异常!");
} else if (ex.getClass().equals(InternalError.class))
{
ex.printStackTrace();
throw new BusinessException("Java虚拟机发生了内部错误");
} else
{
ex.printStackTrace();
throw new BusinessException("程序内部错误,操作失败!" + ex.getMessage());
}
}
}
自定义业务异常处理类 友好提示: package com.pilelot.web.util; /**
* 自定义业务异常处理类 友好提示
* @author Andy Chan
*
*/
public class BusinessException extends RuntimeException
{
private static final long serialVersionUID = 3152616724785436891L; public BusinessException(String frdMessage)
{
super(createFriendlyErrMsg(frdMessage));
} public BusinessException(Throwable throwable)
{
super(throwable);
} public BusinessException(Throwable throwable, String frdMessage)
{
super(throwable);
} private static String createFriendlyErrMsg(String msgBody)
{
String prefixStr = "抱歉,";
String suffixStr = " 请稍后再试或与管理员联系!"; StringBuffer friendlyErrMsg = new StringBuffer(""); friendlyErrMsg.append(prefixStr); friendlyErrMsg.append(msgBody); friendlyErrMsg.append(suffixStr); return friendlyErrMsg.toString();
}
统一日志处理实现类: package com.pilelot.web.util; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger; /**
* Spring 统一日志处理实现类
* @author Andy Chan
*
*/
public class LogInterceptor implements MethodInterceptor
{ public Object invoke(MethodInvocation invocation) throws Throwable
{
Logger loger = Logger.getLogger(invocation.getClass()); loger.info("--Log By Andy Chan -----------------------------------------------------------------------------");
loger.info(invocation.getMethod() + ":BEGIN!--(Andy ChanLOG)");// 方法前的操作
Object obj = invocation.proceed();// 执行需要Log的方法
loger.info(invocation.getMethod() + ":END!--(Andy ChanLOG)");// 方法后的操作
loger.info("-------------------------------------------------------------------------------------------------"); return obj;
} }
Spring配置文件添加:
<!-- Spring 统一日志处理 LogInterceptor拦截器 配置 -->
<bean id="logLnterceptor" class="com.pilelot.web.util.LogInterceptor"/>
<!-- Spring 统一异常处理 ExceptionAdvisor配置 -->
<bean id="exceptionHandler" class="com.pilelot.web.util.ExceptionAdvisor"></bean>
<!-- Bean自动代理处理器 配置-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name="beanNames">
<list> <!-- 配置需要进行日志记录的Service和Dao -->
<value>commonDao</value>
<!-- 配置所有Service结尾命名的Bean,即所有Service层的类都要经过exceptionHandler异常处理类 -->
<value>*Service</value> <!-- Service层的Bean ID 命名要以Service结尾 -->
</list>
</property>
<property name="interceptorNames">
<list>
<value>exceptionHandler</value>
<value>logLnterceptor</value>
<!--<value>transactionInterceptor</value>-->
</list>
</property>
</bean>
<!-- ——————————————————Spring 统一日志处理 + 统一异常处理 配置结束—————————————悲伤的分隔线—————————— -->
}
使用Spring进行统一日志管理 + 统一异常管理的更多相关文章
- 【SpringAop】【统一日志处理】注解方式理解以及使用
[注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...
- SpringMVC -- 梗概--源码--贰--异常管理
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- [译]MVC网站教程(二):异常管理
介绍 “MVC网站教程”系列的目的是教你如何使用 ASP.NET MVC 创建一个基本的.可扩展的网站. 1) MVC网站教程(一):多语言网站框架 2) MVC网站教程(二):异常管理 3) ...
- Spring AOP 自定义注解实现统一日志管理
一.AOP的基本概念: AOP,面向切面编程,常用于日志,事务,权限等业务处理.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容(Spring核心之一),是函数式编程 ...
- Spring MVC 中使用AOP 进行统一日志管理--XML配置实现
1.介绍 上一篇博客写了使用AOP进行统一日志管理的注解版实现,今天写一下使用XML配置实现版本,与上篇不同的是上次我们记录的Controller层日志,这次我们记录的是Service层的日志.使用的 ...
- Spring MVC 中使用AOP 进行统一日志管理--注解实现
1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...
- Spring Boot AOP 扫盲,实现接口访问的统一日志记录
AOP 是 Spring 体系中非常重要的两个概念之一(另外一个是 IoC),今天这篇文章就来带大家通过实战的方式,在编程猫 SpringBoot 项目中使用 AOP 技术为 controller 层 ...
- 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘
前言 请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧. 实现 本篇AOP统一日志管理写法来源于国 ...
- Spring AOP实现统一日志输出
目的: 统一日志输出格式 思路: 1.针对不同的调用场景定义不同的注解,目前想的是接口层和服务层. 2.我设想的接口层和服务层的区别在于: (1)接口层可以打印客户端IP,而服务层不需要 (2)接口层 ...
随机推荐
- winform窗体(二)——控件
一.窗体的事件 每一个窗体都有一个事件,这个窗体加载完成之后执行哪一段代码 位置:1)右键属性→事件→load 双击进入 2)双击窗体任意一个位置进入 删除事件:先将事件页面里面的挂好的事件删除,再删 ...
- Oracle索引梳理系列(八)- 索引扫描类型及分析(高效索引必备知识)
版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...
- YII2 项目安装步骤及异常记录
项目环境: 操作系统:windows 7 版本管理:git 该项目我是在mac上面创建的,mac上面的环境也是一波三折啊!但我同事的环境是在windows上面,因为是前端同学,所以只好我再次操刀了.. ...
- vs配置boost库
步骤: 1.在boost官网下载boost版本,以1.59.0为例. 2.解压,解压后可看到文件夹下有个bootstrap.bat文件. 注意: 如果有以下error: 'cl' 不是内部或外部命令, ...
- 使用Jackson解析Json示例
原文http://blog.csdn.net/gebitan505/article/details/17005735 custom.json: { "country":&q ...
- Oracle用户密码过期和用户被锁解决方法
[原因/触发因素] 确定是由于oracle11g中默认在default概要文件中设置了"PASSWORD_LIFE_TIME=180天"所导致. [影响和风险] 影响 密码过 ...
- 帆软报表FineReport中数据连接的JDBC连接池属性问题
连接池原理 在帆软报表FineReport中,连接池主要由三部分组成:连接池的建立.连接池中连接使用的治理.连接池的关闭.下面就着重讨论这三部分及连接池的配置问题. 1. 连接池原理 连接池技术的核心 ...
- 第4章 文本编辑器vim
1. vim常用操作 1.1 vim简介 (1)vim是一个功能强大的全屏幕文本编辑器,是Linux/Unix上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. (2)vim没有菜单,只有命令 ...
- ThinkPHP常用查询
1.常规 $map2['state'] = 1; $User->where ( $map2 )->find(); 2. OR 查询 $where['name'] = array('neq' ...
- 原创jquery插件treeTable(转)
由于工作需要,要直观的看到某个业务是由那些子业务引起的异常,所以我需要用树表的方式来展现各个层次的数据. 需求: 1.数据层次分明: 2.数据读取慢.需要动态加载孩子节点: 3.支持默认展开多少层. ...