Spring Boot面向切面加注解

一.项目pom.xml文件引入切面依赖

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

二.定义注解类

复制代码
import java.lang.annotation.*;

/**
 * @desc 错误日志注解
 * @author lss
 */
@Target(ElementType.METHOD)   //应用于方法上面
@Retention(RetentionPolicy.RUNTIME)  //表示在运行时注解任可用
public @interface ErrorLog {

    /**
     * 日志报错类型 
     */
    public String type() default "";
}

三.定义切面类,实现方法拦截和异常处理逻辑

复制代码
import com.qike.sys.annotation.ErrorLog;
import com.qike.sys.service.ErrorResponseService;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@RequiredArgsConstructor
public class ErrorLogAspect {

    private final ErrorResponseService errorResponseService;
	
	//切点
    @Around("@annotation(errorLog)")
    public Object around(ProceedingJoinPoint joinPoint, ErrorLog errorLog) throws Throwable {
        // 在方法执行前做些事情
        System.out.println("Before method execution...");

        try {
            // 执行被拦截的方法
            Object result = joinPoint.proceed();
            // 在方法执行后做些事情
            System.out.println("After method execution...");
            return result;
        } catch (Exception e) {
            // 记录错误信息
            // 注解自定义属性获得值
            String type = errorLog.type;
            // 异常逻辑处理
			...
			
            System.out.println("Error occurred: " + e.getMessage());
            throw e;
        }
    }
}

四.在方法中使用

复制代码
	@ErrorLog(type = "2")
    public void splitLogTable(){
    	
    }

四.在拦截器中使用

复制代码
@Slf4j
@Component
public class AppControllerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;

        try {

            log.debug(request.getServletPath());
            // 不用登录的标识
            ErrorLog loginRestrict = handlerMethod.getMethodAnnotation(ErrorLog.class);
            // 未登录可以访问
            if (null != loginRestrict) {
                return true;
            }
			......
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
相关推荐
serve the people16 分钟前
python环境搭建 (六) Makefile 简单使用方法
java·服务器·python
重生之后端学习20 分钟前
146. LRU 缓存
java·数据结构·算法·leetcode·职场和发展
萧曵 丶22 分钟前
懒加载单例模式中DCL方式和原理解析
java·开发语言·单例模式·dcl
回忆是昨天里的海25 分钟前
k8s部署的微服务动态扩容
java·运维·kubernetes
萧曵 丶26 分钟前
单例模式 7 种实现方式对比表
java·单例模式
lang2015092836 分钟前
Tomcat Maven插件全解析:开发部署一体化
java·tomcat·maven
JHC_binge42 分钟前
国内Ubuntu 22.04 LTS安装Milvus向量数据库
java·linux·ubuntu
墨染青竹梦悠然1 小时前
基于Django+vue的图书借阅管理系统
前端·vue.js·后端·python·django·毕业设计·毕设
2501_941148151 小时前
C++ map / multimap 保姆级教程
java·开发语言·c++
Yield & Allure1 小时前
EasyExcel使用
java