AOP在SpringBoot项目中的使用场景解读

 更新时间:2026年01月09日 09:16:30   作者:她又在丛中笑  
本文介绍如何使用AOP在不同场景下对方法执行前进行逻辑校验,包括对整个包下、特定控制器下以及特定注解修饰的方法进行校验,通过自定义注解和切面实现,展示了AOP的灵活性和强大功能

添加DTO

@Data
@Accessors(chain = true)
public class Test {

    private String sex;

    private String name;
}

添加controller(同包不同类)

控制器1

package com.test.controller;
@Slf4j
@RestController
@RequestMapping("/v1/aop_test")
public class AopController {

    @PostMapping("/test")
    public Test test(@RequestBody Test dto) {
        return dto;
    }

    @PostMapping("/testList")
    public List<Test> testList(@RequestBody List<Test> list) {
        return list;
    }
}

控制器2

package com.test.controller;
@Slf4j
@RestController
@RequestMapping("/v1/aop_test_second")
public class ApoTestSecondController {

    /**
     * 根据参数请求接口
     *
     * @return
     */
    @PostMapping(path = "/test")
    public void test(@RequestBody @Valid Test dto) {
        log.info("aop_test_second test {}......",dto.toString());
    }
}

AOP场景演示

以下场景可以叠加。

1. 对某package下的所有接口进行方法执行前逻辑校验

新增切面,编写处理逻辑

@Aspect
@Component
public class AopControllerPackageConfig {
    @Before("execution(* com.test.controller.*.*(..))")
    public void beforeAop(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        System.out.println("AopControllerPackageConfig 方法请求路径:" + request.getRequestURI());// 方法请求路径
        System.out.println("AopControllerPackageConfig 请求方式:" + request.getMethod());// 请求方式
        System.out.println("AopControllerPackageConfig 请求参数:" + JSON.toJSONString(joinPoint.getArgs()));// 请求参数(数组类型)
    }
}

2. 对某controller类下的所有接口进行方法执行前逻辑校验

新增切面,编写处理逻辑

@Aspect
@Component
public class AopControllerConfig {
    @Before("execution(* com.test.controller.AopController.*(..))")
    public void beforeAop(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        System.out.println("AopControllerConfig 方法请求路径:" + request.getRequestURI());// 方法请求路径
        System.out.println("AopControllerConfig 请求方式:" + request.getMethod());// 请求方式
        System.out.println("AopControllerConfig 请求参数:" + JSON.toJSONString(joinPoint.getArgs()));// 请求参数
    }
}

3. 对某注解修饰的所有接口进行方法执行前逻辑校验

自定义注解

package com.test.annotations;

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionValid {
    String value() default "";
}

在控制器1 test 方法前修饰自定义注解

@PermissionValid("我是值")
@PostMapping("/test")
public Test test(@RequestBody Test dto) {
    return dto;
}

新增切面,编写处理逻辑

@Aspect
@Component
public class AopConfig {

    @Before("@annotation(com.test.annotations.PermissionValid)")
    public void beforeAop(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        System.out.println("AopConfig 方法请求路径:" + request.getRequestURI());// 方法请求路径
        System.out.println("AopConfig 请求方式:" + request.getMethod());// 请求方式
        System.out.println("AopConfig 请求参数:" + JSON.toJSONString(joinPoint.getArgs()));// 请求参数
        // 获取目标方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        // 获取方法上的注解
        PermissionValid annotation = method.getAnnotation(PermissionValid.class);
        if (annotation != null) {
            String value = annotation.value(); // 获取注解的value值
            System.out.println("注解值:" + value);
        }
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Mybatis中${param}与#{param}的区别说明

    Mybatis中${param}与#{param}的区别说明

    这篇文章主要介绍了Mybatis中${param}与#{param}的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • SSM框架前后端信息交互实现流程详解

    SSM框架前后端信息交互实现流程详解

    这篇文章主要介绍了SSM框架前后端信息交互实现流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringSecurity怎样使用注解控制权限

    SpringSecurity怎样使用注解控制权限

    这篇文章主要介绍了SpringSecurity怎样使用注解控制权限的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Nacos源码之注册中心的实现详解

    Nacos源码之注册中心的实现详解

    这篇文章主要为大家介绍了Nacos源码之注册中心的实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • SpringBoot中优化if-else语句的七种方法

    SpringBoot中优化if-else语句的七种方法

    if-else语句是控制流程的基本工具,但过度使用会使代码变得复杂且难以维护,在SpringBoot , SpringCloud项目中,优化if-else结构变得尤为重要,本文将深入探讨七种策略,旨在减少SpringBoot , SpringCloud项目中 if-else的使用,需要的朋友可以参考下
    2024-07-07
  • Java矢量队列Vector使用示例

    Java矢量队列Vector使用示例

    Vector类实现了一个动态数组。和ArrayList很相似,但是两者是不同的Vector是同步访问的;Vector包含了许多传统的方法,这些方法不属于集合框架
    2023-01-01
  • java实现登录验证码功能

    java实现登录验证码功能

    这篇文章主要为大家详细介绍了java实现登录验证码功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • MyBatis 三表外关联查询的实现(用户、角色、权限)

    MyBatis 三表外关联查询的实现(用户、角色、权限)

    这篇文章主要介绍了MyBatis 三表外关联查询的实现(用户、角色、权限),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Zookeeper实现分布式锁代码实例

    Zookeeper实现分布式锁代码实例

    这篇文章主要介绍了Zookeeper实现分布式锁代码实例,Zookeeper 分布式锁应用了其 临时顺序节点 的特性,在Zookeeper中创建一个持久节点ParentLock,当第一个客户端要获取锁时,在ParentLock节点下创建一个临时顺序节点,需要的朋友可以参考下
    2023-12-12
  • win10 java(jdk安装)环境变量配置和相关问题

    win10 java(jdk安装)环境变量配置和相关问题

    这篇文章主要介绍了win10java(jdk安装)环境变量配置和相关问题解决,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12

最新评论