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. Sqoop:Could not load db driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver

    Sqoop version:1.4.6-cdh Hadoop version:2.6.0-cdh5.8.2 场景:使用Sqoop从MSSqlserver导数据 虽然1.4.6的官网说 Even if ...

  2. sql server 里面的 dynamic Data Masking

    有时候啊,当我们存放在数据库里面的数据,需要明文存放,但是对于前台查询出来的,又不太希望明文放出去的时候(比方说客户的手机啊,邮箱啊)之类有点敏感的信息,之前通常是保存个明文,然后在前台展现的时候再特 ...

  3. 推荐一个不错的css3网站 可以直接调用的

    animate.css 一搜就能出来  我用着还不错

  4. spring中的定时调度实现TimerFactoryBean引起的隐患

    手中的一个老项目,其中使用的TimerFactoryBean实现的调度任务.一般都是spring quartz实现,这种的着实少见.正因为少见资料比较少,当初为了确认这个会不会2个调度任务同时并行执行 ...

  5. 腾讯 Bugly for Xamarin Android 的插件

    因为项目中需要异常控制,所以在 gpyer bugly 等 Bug 收集平台中选择,最后选定了 Bugly. 于是将 Bugly 的插件 进行了 Java Binding,打成了 Xamarin 可用 ...

  6. js面向对象与原型

    创建对象 var box = new Object();//创建对象 box.name = 'Lee'; //添加属性 box.age = 100; box.run = function(){ ret ...

  7. Hibernate批量处理数据

    01.批量插入数据 步骤一.创建实体类,Dept和Emp /** * 员工类 * @author Administrator * */ public class Emp { private Integ ...

  8. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  9. [tem]线段树(白书版)

    个人感觉有点坑 add用的标记永久化 set用的标记下传 #include <iostream> #include <cstdio> #include <algorith ...

  10. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...