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

如何在AspectJ的execution切入点表达式中指定返回类型TestObj?

调整AspectJ切入点以拦截返回TestObj的方法

嗨,这个问题很好解决,只要修改execution表达式里的返回类型部分就可以啦!

AspectJ的execution语法格式是:execution(返回类型 类路径.方法名(参数列表)),你原来的* *.*(..)里第一个*代表匹配任意返回类型,把它换成你的TestObj(最好用全限定类名,避免类名冲突),就能精准拦截返回值为TestObj的方法了。

结合你现有的代码,修改后的切点应该是这样的:

// 如果TestObj和当前切面类在同一个包下,直接写类名即可
@Around("@annotation(interceptor) && execution(TestObj *.*(..)) && args(argType, ..)") 
public Object logAction(ProceedingJoinPoint joinPoint, Interceptor interceptor, TestObj argType) throws Throwable { 
    // 你的业务逻辑代码
    // 别忘了返回原方法的返回值,@Around方法需要处理返回结果
    return joinPoint.proceed();
}

如果TestObj在其他包下(比如com.yourproject.model.TestObj),一定要写全限定类名,这样AspectJ才能正确识别:

@Around("@annotation(interceptor) && execution(com.yourproject.model.TestObj *.*(..)) && args(argType, ..)") 
public Object logAction(ProceedingJoinPoint joinPoint, Interceptor interceptor, TestObj argType) throws Throwable { 
    // 业务逻辑
    return joinPoint.proceed();
}

简单总结下:

  • 把execution里的第一个通配符*替换成目标返回类型的类名(全限定名更稳妥)
  • 保持其他切点条件(@annotation和args)不变,就能实现只拦截返回TestObj且符合其他条件的方法啦

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:04:33