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. easyui中 combogrid控件的loadData方法加载本地数据

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. solr的copyFeild用法(改变各个feild的权重,修改打分结果)-注意!

    copyField的dest字段all本身有分析器处理:假设为mmseg4j name,title,description三个字段都复制到all字段上:其中title和description都是mms ...

  3. sql如何选取两个数据表中的值

    一.直接在要选择的数据前面加上数据表的名字就行了 SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Order ...

  4. 分布式锁2 Java非常用技术方案探讨之ZooKeeper 【转载】

    前言:       由于在平时的工作中,线上服务器是分布式多台部署的,经常会面临解决分布式场景下数据一致性的问题,那么就要利用分布式锁来解决这些问题.以自己结合实际工作中的一些经验和网上看到的一些资料 ...

  5. 10.model/view实例(3)

    任务:3x2的表格,第一个单元格显示当前时间 思考: 1.data函数里面QTime::currentTime()显示当前时间 2.但是这个事件是一个固定的时间,不会变动 3.需要时间变动,View就 ...

  6. Entity Framework Tutorial Basics(17):DBSet Class

    DBSet Class DBSet class represents an entity set that is used for create, read, update, and delete o ...

  7. html 连接数据库

    http://blog.csdn.net/haxker/article/details/4214001 http://www.cnblogs.com/chuncn/archive/2010/11/22 ...

  8. 什么是DMZ、DMZ端口、DMZ主机?

    DMZ是英文“demilitarized zone”的缩写,中文名称为“隔离区”,也称“非军事化区”.它是为了解决安装防火墙后外部网络不能访问内部网络服务器的问题,而设立的一个非安全系统与安全系统之间 ...

  9. 判断wifi连接是否可用

    /*** 判断当前连接方式是否是WIFI连接* * @param context* @return*/private static boolean isWifiConnected(Context co ...

  10. [译]Javascript中的Ternary operator

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...