方式1:使用tt命令获取到spring context
步骤1:使用时间隧道记录请求并用HTTP工具随便发一个请求
[arthas@18312]$ tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 58 ms, listenerId: 5
INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1004 2023-11-29 17:00:29 78.0642 true false 0x5beda06e RequestMappingHandlerAdapter invokeHandlerMethod
步骤2:用tt命令的-i参数来指定index,并且用-w参数来执行ognl表达式来获取spring context
[arthas@18312]$ tt -i 1004 -w 'target.getApplicationContext()'
@AnnotationConfigServletWebServerApplicationContext[
reader=@AnnotatedBeanDefinitionReader[org.springframework.context.annotation.AnnotatedBeanDefinitionReader@662dc31d],
scanner=@ClassPathBeanDefinitionScanner[org.springframework.context.annotation.ClassPathBeanDefinitionScanner@aa8b818]
...省略
步骤3:为所欲为
tt -i 1004 -w 'target.getApplicationContext().getBean("memberService").getByGlobalId("123")'
方式2:使用ognl表达式获取
在很多代码里都有static函数或者工具类,以Hutool的SpringUtil为例,可以使用
# 不使用变量
[arthas@18312]$ ognl "@cn.hutool.extra.spring.SpringUtil@getBean('memberService').getByGlobalId('123')"
# 使用变量
[arthas@18312]$ ognl "#context=@cn.hutool.extra.spring.SpringUtil@getApplicationContext(),#context.getBean('memberService').getByGlobalId('123')"