不希望通过编码的方式,想通过arthas 获取spring 属性。
实际执行时总是报找不到类, 但是通过sc 命令可以找到该类
[arthas@312]$ ognl '#context=@com.xxx.ApplicationContextHelper@appCtx,#context.getEnvironment().getProperty("author")' Failed to execute ognl, exception message: ognl.OgnlException: Could not get static field appCtx from class com.xxx.ApplicationContextHelper [java.lang.ClassNotFoundException: Unable to resolve class: com.xxx.ApplicationContextHelper], please check $HOME/logs/arthas/arthas.log for more details. [arthas@312]$ sc com.xxx.ApplicationContextHelper com.xxx.ApplicationContextHelper2 原因分析
搜到了 arthas 对该问题解释,大意是自定义类加载器导致找不到类
https://github.com/alibaba/arthas/issues/71
解决办法:
第一步,查询指定类的classloader信息,命令为:sc -d xxxx,根据结果可以看到类对应classloader的hash值;
第二步,ognl命令指定classloader,命令为:ognl -c xxxx(classloader的hash值) xxx(表达式)
- su app
- as.sh
- sc -d com.xxx.ApplicationContextHelper
找到 classLoaderHash 41a2befb - ognl -c 41a2befb ‘#context=@com.xxx.ApplicationContextHelper@appCtx,#context.getEnvironment().getProperty(“druid.datasource.password”)’