栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

SpringAOP----捕捉异常存进数据库,方便找到问题

Java 更新时间:发布时间: 百科书网 趣学号

在日常开发的时候,有时候会出错,这个时候如果去看日志,会有一大堆日志查找很困难,所以就用spring的Aop来捕捉异常,存进数据库

切入点

加上一些信息,方便更加快速定位错误信息

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
public @interface LogAnnotation {
    
    public String title() default " ";
    
    public BusinessType businessTyp() default BusinessType.INSERT;

}
@Aspect
@Component
public class LogExceptionAspectj extends RuntimeException{
    @Autowired
    private ExceptionHandleService exceptionHandleService;

    @AfterThrowing(value = "@annotation(com.twoblog.twoblogmaster.annotation.LogAnnotation)",throwing = "e")
    public void exceptionHandle(JoinPoint joinPoint,Exception e){
        //获取异常类型
        String exceptionType = e.getClass().getName();
        //获取异常信息
        String message = ExceptionUtil.getMessage(e);
        //获取异常的参数
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        LogAnnotation annotation = signature.getMethod().getAnnotation(LogAnnotation.class);
        BusinessType businessType = annotation.businessTyp();
        String s = businessType.toString();
        String title = annotation.title();
        //获取到出错的路径
        String errorPath = joinPoint.getTarget().getClass().getName()+"."+signature.getName();
        ExceptionHandle build = ExceptionHandle.builder().message(message).methodTitle(title).methodFunction(s).exceptionType(exceptionType).exceptionPath(errorPath).build();
        exceptionHandleService.save(build);
    }
}

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/848370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号