使用arthas中vmtool命令查看spring容器中对象的某个属性
场景:
线上环境我想查看spring中容器某个对象的属性值
vmtool命令
方式一:
vmtool --action getInstances -c [类加载器的hash] --className [目标类全路径] --limit 10 -x 2
实例:查询该类的全部属性情况(该类是一个spring中容器的对象,因此可以直接可以看到线上运行的属性值情况)
vmtool --action getInstances -c 7637f22 --className com.gisquest.cloud.oauth2.authentication.GisqAuthenticationSuccessHandler --limit 10 -x 2
方式二:
语法:
vmtool --action getInstances -c [类加载器的hash] --className [org.springframework.web.context.WebApplicationContext]--express 'instances[0].getBean("对象名")'
实例:查询spring容器中的某个对象
vmtool --action getInstances -c [ 7637f22] --className [org.springframework.web.context.WebApplicationContext]--express 'instances[0].getBean("gisqAuthenticationSuccessHandler")'
语法:
vmtool --action getInstances -c 7637f22 --className 【容器全路径】--express 'instances[0].getBean("实例对象名").【属性名】'
实例:查询spring容器中的某个对象的某个属性值
vmtool --action getInstances -c 7637f22 --className org.springframework.web.context.WebApplicationContext --express 'instances[0].getBean("gisqAuthenticationSuccessHandler").needUpdatePwdTime'
可能遇到的问题 :
问题一:参照官网直接复制 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
vmtool --action getInstances --className org.springframework.context.ApplicationContext --express 'instances[0].getBean("gisqAuthenticationSuccessHandler")'
Failed to execute ognl, exception message: ognl.MethodFailedException: Method "getBean" failed for object org.springframework.context.annotation.AnnotationConfigApplicationContext@91161c7, started on Tue Feb 07 11:08:21 CST 2023 [org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'gisqAuthenticationSuccessHandler' available], please check $HOME/logs/arthas/arthas.log for more details.
[arthas@1]$
原因:在实际的项目中往往是web容器,因此容器全类名需要根据实际情况改动。
vmtool --action getInstances --className org.springframework.web.context.WebApplicationContext --express 'instances[0].getBean("gisqAuthenticationSuccessHandler")'
问题二:获取当前类是那个类加载器加载的
sc -d 【目标类】
实例:
sc -d com.gisquest.cloud.oauth2.authentication.GisqAuthenticationSuccessHandler