一个大型的Spring工程,多人协作,线上环境,自己的Bean被人AOP了,现在功能不正常,怎么快速定位?

文章讨论了在大型Spring项目中多人协作可能导致的AOP配置问题,提出使用Arthas工具观测AOPAdvice和PointCut,以便于定位和解决线上动态生效的AOP影响问题。

一个大型的Java的Spring工程,是会存在多人协作的情况。

一个大型Spring工程里,往往充斥了各种全局的IOC容器的配置,可能有人自己写了一段AOP的代码,但是PointCut写的很随意【例如:通过..的通配符直接进行全路径的统配】,导致无意间对其他人的Bean产生了影响。

如果这段配置是动态生效的,本地开发环境不生效,线上生产环境生效,那问题排查就非常麻烦。

这里提供一种方法,通过arthas去观测spring的应用,通过观测AdvisedSupport类的getInterceptorsAndDynamicInterceptionAdvice的方法,去观测具体生效的AOP的Advice具体是什么。

可以通过在线工具,直接生成命令: 观测AOP的命令

直接 输入要观测的Bean的名字 和 类的全路径即可。

例如: methodName 填写: loadData

className 填写:org.example.springboot.controller.DataController

-n 是观测多少次,可以根据实际情况自行改写。

-x 是打印对象的层级,不要动,3层就可以了。

最终打印出的结果类似如下:

Affect(class count: 3 , method count: 1) cost in 57 ms, listenerId: 5
method=org.springframework.aop.framework.AdvisedSupport.getInterceptorsAndDynamicInterceptionAdvice location=AtExit
ts=2023-08-03 13:10:17; [cost=0.0265ms] result=@ArrayList[
    @ArrayList[
        @ExposeInvocationInterceptor[
            INSTANCE=@ExposeInvocationInterceptor[org.springframework.aop.interceptor.ExposeInvocationInterceptor@5a271367],
            ADVISOR=@[org.springframework.aop.interceptor.ExposeInvocationInterceptor.ADVISOR],
            invocation=@NamedThreadLocal[Current AOP method invocation],
        ],
        @InterceptorAndDynamicMethodMatcher[
            interceptor=@AspectJAroundAdvice[org.springframework.aop.aspectj.AspectJAroundAdvice: advice method [public java.lang.Object org.example.springboot.aop.MyAspect.withAnnotation(org.aspectj.lang.ProceedingJoinPoint) throws java.lang.Throwable]; aspect name 'myAspect'],
            methodMatcher=@AspectJExpressionPointcut[AspectJExpressionPointcut: () @within(org.example.springboot.aop.annotations.AopAnnotation)],
        ],
    ],
]

其中,应该关注 InterceptorAndDynamicMethodMatcher 之类的输出:

其中 interceptor=@AspectJAroundAdvice  后面就是AOP增强的方法:

methodMatcher=@AspectJExpressionPointcut 后面就是哪个PointCut命中了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值