You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring AOP切入点表达式如何排除指定子包?

解决方案:扫描基础包并排除所有含dto的子包

针对你的需求——扫描com.spring下所有子包,同时排除任何名称包含dto的子包(包括多级嵌套的如com.spring.service.dto.v2),可以使用**within()**结合execution()的组合表达式,实现精准且通用的排除逻辑:

String basePkg = "com.spring";
String pointcut = "execution(* " + basePkg + "..*.*(..)) && !within(*..dto..*)";

为什么之前的写法不生效?

你之前尝试的!execution(* *.dto.*(..))或!execution(* com.spring..*.dto.*.*(..))存在两个核心问题:

  • 仅排除了dto包下直接定义的方法,没覆盖到dto子包(比如dto.v2)里的类
  • execution匹配的是方法执行行为,若dto包下的类被其他包调用,这种排除逻辑会有遗漏;而within()是直接匹配类所在的包范围,排除更彻底

表达式细节说明

  • execution(* com.spring..*.*(..)):匹配com.spring及其所有子包下的所有类的所有方法
  • !within(*..dto..*):排除所有位于名称包含dto的包(包括其子包)下的类,不管dto在包路径的哪个层级

适配你的项目结构验证

  • 会命中com.spring.controller、com.spring.service、com.spring.entity等非dto包下的方法
  • 会自动排除com.spring.service.dto、com.spring.repo.dto.v2、com.spring.service.dto.v2等所有含dto的包下的方法

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 20:25:27