java基于spring注解AOP的异常处理的方法

发布时间 - 2026-01-10 23:01:23    点击率:

一、前言

项目刚刚开发的时候,并没有做好充足的准备。开发到一定程度的时候才会想到还有一些问题没有解决。就比如今天我要说的一个问题:异常的处理。写程序的时候一般都会通过try...catch...finally对异常进行处理,但是我们真的能在写程序的时候处理掉所有可能发生的异常吗? 以及发生异常的时候执行什么逻辑,返回什么提示信息,跳转到什么页面,这些都是要考虑到的。

二、基于@ControllerAdvice(加强的控制器)的异常处理

@ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。本例子中使用ExceptionHandler应用到所有@RequestMapping注解的方法,处理发生的异常。

示例代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hjz.exception.ServiceException;
import com.hjz.exception.utils.ExceptionUtils;

@ResponseBody
public class ExceptionAdvice {
 private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionAdvice.class);

 /**
  * 拦截web层异常,记录异常日志,并返回友好信息到前端
  * 目前只拦截Exception,是否要拦截Error需再做考虑
  *
  * @param e 异常对象
  * @return 异常提示
  */
 @ExceptionHandler(Exception.class)
 public ResponseEntity<String> handleException(Exception e) {
  //不需要再记录ServiceException,因为在service异常切面中已经记录过
  if (!(e instanceof ServiceException)) {
   LOGGER.error(ExceptionUtils.getExcTrace(e));
  }

  HttpHeaders headers = new HttpHeaders();
  headers.set("Content-type", "text/plain;charset=UTF-8");
  headers.add("icop-content-type", "exception");
  String message = StringUtils.isEmpty(e.getMessage()) ? "系统异常!!" : e.getMessage();
  return new ResponseEntity<>(message, headers, HttpStatus.OK);
 }
}

如果不起作用,请检查 spring-mvc的配置文件,是否有ControllerAdvice的如下配置

<context:component-scan base-package="com.sishuok.es" use-default-filters="false"> 
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
  <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 
 </context:component-scan> 

三、基于AOP的异常处理

1.处理controller层的异常 WebExceptionAspect.java

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hjz.exception.ServiceException;
import com.hjz.exception.utils.ExceptionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * web异常切面
 * 默认spring aop不会拦截controller层,使用该类需要在spring公共配置文件中注入改bean,
 * 另外需要配置<aop:aspectj-autoproxy proxy-target-class="true"/>
 */
@Aspect
public class WebExceptionAspect {
 private static final Logger LOGGER = LoggerFactory.getLogger(WebExceptionAspect.class);

 @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
 private void webPointcut() {}

 /**
  * 拦截web层异常,记录异常日志,并返回友好信息到前端
  * 目前只拦截Exception,是否要拦截Error需再做考虑
  *
  * @param e 异常对象
  */
 @AfterThrowing(pointcut = "webPointcut()", throwing = "e")
 public void handleThrowing(Exception e) {
  //不需要再记录ServiceException,因为在service异常切面中已经记录过
  if (!(e instanceof ServiceException)) {
   LOGGER.error(ExceptionUtils.getExcTrace(e));
  }

  String errorMsg = StringUtils.isEmpty(e.getMessage()) ? "系统异常" : e.getMessage();
  writeContent(errorMsg);
 }

 /**
  * 将内容输出到浏览器
  *
  * @param content 输出内容
  */
 private void writeContent(String content) {
  HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
  response.reset();
  response.setCharacterEncoding("UTF-8");
  response.setHeader("Content-Type", "text/plain;charset=UTF-8");
  response.setHeader("icop-content-type", "exception");
  PrintWriter writer = null;
  try {
   writer = response.getWriter();
  } catch (IOException e) {
   e.printStackTrace();
  }
  writer.print(content);
  writer.flush();
  writer.close();
 }
}

2.处理service层的异常ServiceExceptionAspect .java

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hjz.exception.ServiceException;
import com.hjz.exception.utils.ExceptionUtils;

@Aspect
public class ServiceExceptionAspect {
 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceExceptionAspect.class);

 /**
  * @within(org.springframework.stereotype.Service),拦截带有 @Service 注解的类的所有方法
  * @annotation(org.springframework.web.bind.annotation.RequestMapping),拦截带有@RquestMapping的注解方法
  */
 @Pointcut("@within(org.springframework.stereotype.Service) && execution(public * *(..))")
 private void servicePointcut() {}

 /**
  * 拦截service层异常,记录异常日志,并设置对应的异常信息
  * 目前只拦截Exception,是否要拦截Error需再做考虑
  *
  * @param e 异常对象
  */
 @AfterThrowing(pointcut = "servicePointcut()", throwing = "e")
 public void handle(JoinPoint point, Exception e) {
  LOGGER.error(ExceptionUtils.getExcTrace(e));

  String signature = point.getSignature().toString();
  String errorMsg = getMessage(signature) == null ? (StringUtils.isEmpty(e.getMessage()) ? "服务异常" : e.getMessage()) : getMessage(signature);
  throw new ServiceException(errorMsg, e);
 }

 /**
  * 获取方法签名对应的提示消息
  *
  * @param signature 方法签名
  * @return 提示消息
  */
 private String getMessage(String signature) {
  return null;
 }
}

3.使用方式,在spring的公共配置文件中加入如下配置:

<aop:aspectj-autoproxy proxy-target-class="true" />
<bean class="com.hjz.exception.aspect.ServiceExceptionAspect" />
<bean class="com.hjz.exception.aspect.WebExceptionAspect" />

或者 自定义一个 注册类,ServiceExceptionAspect.java和WebExceptionAspect.java都加入@Component注解

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 异常相关bean注册类
 */
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.hjz.exception.aspect")
public class ExceptionConfig {

}
@Aspect
@Component
public class WebExceptionAspect {
 .......... 
}


@Aspect
@Component
public class ServiceExceptionAspect {
 .........
}

四、疑惑

@within(org.springframework.stereotype.Service),拦截带有 @Service 注解的类的所有方法

@annotation(org.springframework.web.bind.annotation.RequestMapping),拦截带有@RquestMapping的注解方法

五、测试

分别编写controller层和service层的异常测试类。这个很简单,在方法里简单的抛一下异常就可以了。最后验证一下,异常发生的时候有没有 执行 @AfterThrowing对应的方法就好了。具体还是看我写的demo吧,嘿嘿嘿!!!

完整项目下载地址:Spring-AOP_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# spring  # aop异常处理  # spring4  # aop处理异常  # java Aop实现自动填充字段值示例  # java开发AOP基础JdkDynamicAopProxy  # java开发AOP面向切面编程入门  # Java Spring AOP之PointCut案例详解  # Java aop面向切面编程(aspectJweaver)案例详解  # Java JDK动态代理(AOP)用法及实现原理详解  # java捕获AOP级别的异常并将其传递到Controller层  # 再做  # 配置文件  # 不需  # 要再  # 下载地址  # 才会  # 能在  # 提示信息  # 看我  # 很简单  # 考虑到  # 自定义  # 一个问题  # 这些都是  # 还有一些  # 可能发生  # 就比  # 大家多多  # 就可以  # 跳转到 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】  通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】  制作电商网页,电商供应链怎么做?  Windows10如何更改计算机工作组_Win10系统属性修改Workgroup  JS实现鼠标移上去显示图片或微信二维码  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  Win11怎样安装网易有道词典_Win11安装词典教程【步骤】  laravel怎么配置Redis作为缓存驱动_laravel Redis缓存配置教程  如何快速选择适合个人网站的云服务器配置?  如何在建站之星绑定自定义域名?  Laravel如何处理文件上传_Laravel Storage门面实现文件存储与管理  Laravel怎么使用Collection集合方法_Laravel数组操作高级函数pluck与map【手册】  如何快速完成中国万网建站详细流程?  油猴 教程,油猴搜脚本为什么会网页无法显示?  Laravel观察者模式如何使用_Laravel Model Observer配置  Laravel如何优化应用性能?(缓存和优化命令)  网站制作大概多少钱一个,做一个平台网站大概多少钱?  如何快速生成高效建站系统源代码?  UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】  使用C语言编写圣诞表白程序  Android 常见的图片加载框架详细介绍  Laravel的契約(Contracts)是什么_深入理解Laravel Contracts与依赖倒置  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  如何撰写建站申请书?关键要点有哪些?  高端建站如何打造兼具美学与转化的品牌官网?  如何解决hover在ie6中的兼容性问题  音乐网站服务器如何优化API响应速度?  如何快速搭建FTP站点实现文件共享?  JS经典正则表达式笔试题汇总  Laravel如何实现本地化和多语言支持?(i18n教程)  微信小程序 配置文件详细介绍  Laravel如何实现API速率限制?(Rate Limiting教程)  ,交易猫的商品怎么发布到网站上去?  Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】  Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能  独立制作一个网站多少钱,建立网站需要花多少钱?  JS去除重复并统计数量的实现方法  php json中文编码为null的解决办法  Laravel怎么防止CSRF攻击_Laravel CSRF保护中间件原理与实践  Laravel API资源类怎么用_Laravel API Resource数据转换  Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧  Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区  再谈Python中的字符串与字符编码(推荐)  IOS倒计时设置UIButton标题title的抖动问题  ChatGPT常用指令模板大全 新手快速上手的万能Prompt合集  Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】  Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  智能起名网站制作软件有哪些,制作logo的软件?  Laravel怎么进行浏览器测试_Laravel Dusk自动化浏览器测试入门  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?