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进行统一日志管理 + 统一异常管理的更多相关文章

  1. 【SpringAop】【统一日志处理】注解方式理解以及使用

    [注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...

  2. SpringMVC -- 梗概--源码--贰--异常管理

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  3. [译]MVC网站教程(二):异常管理

    介绍 “MVC网站教程”系列的目的是教你如何使用 ASP.NET MVC 创建一个基本的.可扩展的网站. 1)   MVC网站教程(一):多语言网站框架 2)   MVC网站教程(二):异常管理 3) ...

  4. Spring AOP 自定义注解实现统一日志管理

    一.AOP的基本概念: AOP,面向切面编程,常用于日志,事务,权限等业务处理.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容(Spring核心之一),是函数式编程 ...

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

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

  6. Spring MVC 中使用AOP 进行统一日志管理--注解实现

    1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...

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

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

  8. 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘

    前言 请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧. 实现 本篇AOP统一日志管理写法来源于国 ...

  9. Spring AOP实现统一日志输出

    目的: 统一日志输出格式 思路: 1.针对不同的调用场景定义不同的注解,目前想的是接口层和服务层. 2.我设想的接口层和服务层的区别在于: (1)接口层可以打印客户端IP,而服务层不需要 (2)接口层 ...

随机推荐

  1. TCP Provider The semaphore timeout period has expired

    我们一数据库服务器上有个作业最近几天偶尔会遇到下面错误(敏感信息已做处理),主要是报"TCP Provider: The semaphore timeout period has expir ...

  2. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  3. 从零自学Hadoop(07):Eclipse插件

    阅读目录 序 Eclipse Eclipse插件 新建插件项目 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写 ...

  4. SQL Server自动化运维系列——关于数据收集(多服务器数据收集和性能监控)

    需求描述 在生产环境中,很多情况下需要采集数据,用以定位问题或者形成基线. 关于SQL Server中的数据采集有着很多种的解决思路,可以采用Trace.Profile.SQLdiag.扩展事件等诸多 ...

  5. 基于注解的SpringMVC

    相比传统的继承Controller体系中某些类的方式,SpringMVC的注解具有以下优点: 1.Controller不再需要继承某个特定类,只是简单的POJO. 2.请求映射的配置非常方便灵活. 3 ...

  6. Spark官方文档 - 中文翻译

    Spark官方文档 - 中文翻译 Spark版本:1.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 引入Spark(Linki ...

  7. Linux下java进程CPU占用率高-分析方法

    今天登陆同事的一台gateway 开始以为hive环境登陆不了了,仔细一看看了下是因为机器很卡,我每次等几秒没登陆就ctrl+c了,看了下是有个java进程cpu:340.4%  mem:14.6%  ...

  8. shell脚本中生成延时

    #!/bin/bash echo -n count: tput sc count=; while true; do ]; then let count++; ; tput rc tput ed ech ...

  9. Visual Studio SetSite failed for package [JavaScriptWebExtensionsPackage] 错误解决方案一则

    安装 AspNet5.ENU.RC1.exe Microsoft ASP.NET and Web Tools 2015 (RC) – Visual Studio 2015 打开VS后发生了错误 < ...

  10. vim 命令详解

    vi: Visual Interface 可视化接口vim: VI iMproved VI增强版 全屏编辑器,模式化编辑器 vim模式: 编辑模式(命令模式) 输入模式 末行模式 模式转换: 编辑-- ...