使用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)接口层 ...
随机推荐
- JavaScript 易错知识点整理
本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函数参数,闭包问题及对象拷贝与赋值这6个方面进行由浅入深的介绍和讲解,其中也涉及了一些ES ...
- forward和redirect的区别(转)
Redirect Forward 不同的request 不同的对象,但是可以渠道上一个页面的内容 send后面的语句会继续执行,除非return Forward后面的语句不会继续发送给客户端 速度慢 ...
- 今天主要推荐一下django学习的网址!
前言:每个月忙碌的头20天后,在上班时间投入到django理论学习的过程中,花了差不多3天时间简单的研究了一下django,着实废了我不少脑细胞. 采用虫师前辈的一张图和话: 如果你把这过程梳理清晰了 ...
- jquery选择器案例
一.预期效果 实现一个效果,如下. 品牌列表默认精简显示,单击“显示全部品牌”按钮显示全部品牌,同时列表将推荐的品牌的名字高亮显示,按钮里的文字变成“精简显示品牌”.再次点击“精简显示品牌”回到初始页 ...
- 【2016-11-3】【坚持学习】【Day18】【Oracle 数据类型 与C#映射关系】
大部分类型的对应关系:原文:http://2143892.blog.51cto.com/2133892/499353 序号 Oracle数据类型 .NET类型 GetOracleValue类型 DbT ...
- Android UI控件----ExpandableListView的基本用法
ExpandableListView介绍 ExpandableListView的引入 ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListVie ...
- NYOJ 333
http://www.cppblog.com/RyanWang/archive/2009/07/19/90512.aspx?opt=admin 欧拉函数 E(x)表示比x小的且与x互质的正整数的个数. ...
- web安全性测试用例
建立整体的威胁模型,测试溢出漏洞.信息泄漏.错误处理.SQL 注入.身份验证和授权错误. 1. 输入验证 客户端验证 服务器端验证(禁用脚本调试,禁用Cookies) 1.输入很大的数(如4,29 ...
- 前端之css
前端之css 本节内容 css概述及引入 css选择器 css常用属性 1.css概述及引入 CSS概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数 ...
- 简单了解ICMP协议
ping命令是什么协议? 维基百科: ping是一种电脑网络工具,用来测试数据包能否通过IP协议到达特定主机.ping的运作原理是向目标主机传出一个ICMP echo@要求数据包,并等待接受echo回 ...