spring 中 的 异常处理 使用的是aspectj

@Aspect
@Component
/**
*
* @author ****
* @createData 2017年7月13日 上午8:36:23
* @说明 :出了一些空值。。。
*/
public class AjaxEntityHandler { // @Pointcut("@annotation(org.zkdg.utils.annotations.AfterHandler)") @Pointcut("@annotation(org.zkdg.utils.spring.annotations.NotNullVariable)")
// @Pointcut("execution(* org.dcexam.*.service.*.*(..))")
public void beforeCall() {
// service方法调用之前,检测参数,仅限第一个参数, 不能为空值
} /**
* service发生异常时调用
*/
@Pointcut("execution(* org.dcexam.*.service.*.*(..))")
public void afterThrowEx() {
System.out.println("************\n\n\n\n\n\n\n\n\n\n\n\n*******");
} @Around(value = "beforeCall()")
public AjaxEntity doBefore(ProceedingJoinPoint point) throws Throwable {
// TODO Auto-generated method stub
Object[] args = point.getArgs();
if (args == null || args[0] == null) {
return new AjaxEntity("warning", "未选择任何数据。。。");
}
if (args[0] instanceof String) {
String str = (String) args[0];
if (str.equalsIgnoreCase(""))
return new AjaxEntity("warning", "未选择任何数据。。。");
} AjaxEntity ajax = (AjaxEntity) point.proceed(args); return ajax == null ? AjaxEntity.ERROR("操作失败") : ajax;
} /**
*
* @param joinPoint
* 连接点
* @param ex
* 异常
* @return AjaxEntity 异常信息
*/
@AfterThrowing(value = "afterThrowEx()", throwing = "ex")
public void doAfterThrowEx(JoinPoint joinPoint, Exception ex) {
AjaxEntity ajax = new AjaxEntity(); if (ex.getCause() instanceof SQLException) {
// 数据库操作异常
ajax = AjaxEntity.ERROR("操作数据库时出现异常");
} } }
spring.xml 中 配置

<!-- 注解aop,支持注解 -->
<aop:aspectj-autoproxy />

事务 切点配置

<!-- 配置参与事务的类 -->
<aop:config expose-proxy="true" proxy-target-class="true">
<aop:pointcut id="txPointCut"
expression="execution(* org.dcexam.*.service.*.*(..))" />
<aop:advisor pointcut-ref="txPointCut" advice-ref="txAdvice" />
</aop:config>

注意   expose-proxy="true" proxy-target-class="true" 是 aspectj 代理  

在springMvc 中 ,由于 spring 与 springmvc 为 不同的容器。尽量不要使用aspecj代理  ,使用spring mvc 自带的 HandlerExceptionResolver 处理 异常


package org.zkdg.utils.spring.interceptor;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.zkdg.utils.entity.AjaxEntity;
import org.zkdg.utils.util.JsonUtils; /**
*
* @author
* @createData 2017年7月13日 下午12:27:19
* @说明 :springMvc 异常处理
*/
// 注解,被spring 扫描到
@Component
public class ExInterceptor implements HandlerExceptionResolver { @Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
// TODO Auto-generated method stub
if (ex != null) {
try {
response.setStatus(200);
Throwable cause = ex.getCause();
if (cause == null) {
response.getWriter().write(JsonUtils.objectToJson(AjaxEntity.ERROR("<p style='color:red'>空指针异常</p> ")));
} else if (cause instanceof SQLException) {
// json 输出
response.getWriter()
.write(JsonUtils.objectToJson(AjaxEntity.ERROR("数据库操作失败 : <p style='color:red'>" + cause.getMessage()+"</p>")));
} else if (cause instanceof NullPointerException) {
response.getWriter()
.write(JsonUtils.objectToJson(AjaxEntity.ERROR("空指针异常 : <p style='color:red'>" + cause.getMessage()+"</p>")));
} else {
response.getWriter()
.write(JsonUtils.objectToJson(AjaxEntity.ERROR("未知异常 : <p style='color:red'>" + cause.getMessage()+"</p>")));
}
} catch (Exception e) {
// TODO Auto-generated catch block e.printStackTrace();
} }
ex.printStackTrace();
// 返回一个空的 modelandview(),必须返回,否则 异常处理配置无效。。
return new ModelAndView();
} }

 

不得不说,spring 是真的太强大了!!!!

spring 和 spirngMvc 中 异常处理的更多相关文章

  1. Spring Cloud Gateway中异常处理

    最近我们的项目在考虑使用Gateway,考虑使用Spring Cloud Gateway,发现网关的异常处理和spring boot 单体应用异常处理还是有很大区别的.让我们来回顾一下异常. 关于异常 ...

  2. springcloud Zuul中异常处理细节

    Spring Cloud Zuul对异常的处理整体来说还是比较方便的,流程也比较清晰,只是由于Spring Cloud发展较快,各个版本之间有差异,导致有的小伙伴在寻找这方面的资料的时候经常云里雾里, ...

  3. 转:Spring Boot应用中的异常处理

    引自:https://www.cnblogs.com/yangfanexp/p/7616570.html 楼主前几天写了一篇“Java子线程中的异常处理(通用)”文章,介绍了在多线程环境下3种通用的异 ...

  4. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  5. SpringMVC中异常处理详解

    Spring MVC处理异常最基本的就是HandlerExceptionResolver这个接口,先看张图 分析上图可以轻松总结出,spring mvc里有三种异常处理方法: 1.使用官方提供的简单异 ...

  6. Spring cloud Zuul网关异常处理

    Spring cloud Zuul网关异常处理 一 异常测试: 1> 创建一个pre类型的过滤器,并在该过滤器的run方法实现中抛出一个异常.比如下面的实现,在run方法中调用的doSometh ...

  7. spring 或 springboot统一异常处理

    spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...

  8. 基于Spring Boot的统一异常处理设计

    基于Spring Boot的统一异常处理设计 作者: Grey 原文地址:https://www.cnblogs.com/greyzeng/p/11733327.html Spring Boot中,支 ...

  9. 在Spring tools suite中使用git 共享项目

    我们都在eclipse 和 myeclipse中使用过cvs 和 svn 版本控制工具进行团队开发,今天我学习了另外一种版本控制工具git,下面我演示如何在Spring tools suite中使用g ...

随机推荐

  1. Shell编程进阶 1.6 if判断的几种用法

    针对文件和目录的逻辑判断 touch .txt .txt ]; then echo ok;fi -f 判断1.txt是否是文件且是否存在,成立输出ok if [-d /tmp/ ]; then ech ...

  2. 配置php的curl模块问题

    问题 checking for cURL in default path... not foundconfigure: error: Please reinstall the libcurl dist ...

  3. DAY16-Django之MTV

    MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Template(模版):负责如何把页面展示给用户 View(视图):负责业务逻辑,并在适当的时候 ...

  4. 嵌入式Linux启动优化手记2&nbsp;U…

    参考一下 原文地址:U-boot优化">嵌入式Linux启动优化手记2 U-boot优化作者:ZhaoJunling 既然不能使用新的U-boot,那就优化一点是一点,慢慢干吧. 1. ...

  5. Android studio导入svn工程

    Quick Start——> Check outproject from Version——> Subversion——> ‘+’加号 ——> 输入网址 ——> 注意选择 ...

  6. 【转】TinyMCE(富文本编辑器)

    效果预览:http://www.tinymce.com/tryit/full.php [转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 转自:http://www.cnblogs.co ...

  7. ruby 【rails在win7_64位操作系统安装】

    gem update --system --source http://production.s3.rubygems.org 安装截图

  8. OpenStack基础及概念

    一.云计算基本概念解析        1.1什么是云计算 云计算:代表计算资源向云水循环一样,按需分配,循环利用. 1.2.云计算分类 狭义:IT基础设施的交互和使用模式,通过网络以按需,易扩展的方式 ...

  9. PCL—点云分割(最小割算法) 低层次点云处理

    1.点云分割的精度 在之前的两个章节里介绍了基于采样一致的点云分割和基于临近搜索的点云分割算法.基于采样一致的点云分割算法显然是意识流的,它只能割出大概的点云(可能是杯子的一部分,但杯把儿肯定没分割出 ...

  10. Boost中实现线程安全

    博客转载自: http://www.cnblogs.com/lvdongjie/p/4447142.html 1 boost原子变量和线程 #include <boost/thread.hpp& ...