对基于spring的工程而言,获取到spring上下文就可以做很多事情,比如获取注册的Bean对象,查看其属性值,调用其方法等。生产环境中排查问题时,不方便修改代码打印变量的值、重启服务以便调用刷新缓存的方法等,这时,使用arthas就方法许多了。
基于SpringMvc的工程
- 获取spring上下文
tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod -n 3
- 获取Bean对象
tt -i 1000 -w 'target.getApplicationContext().getBean("serviceBTRelationServiceImpl")'
- 如果Bean是AOP代理后的对象,如何获取目标对象呢
tt -i 1000 -w 'target.getApplicationContext().getBean("serviceBTRelationServiceImpl").getTargetSource()'