[alibaba/arthas]无法通过ognl调用父类的方法

2024-04-24 631 views
5
环境信息
  • Arthas 版本: 3.5.5
  • 操作系统版本: win10
  • 目标进程的JVM版本: jdk8
重现问题的步骤

子类继承父类,子类是bean,父类不是bean,选择想通过ognl调用父类的方法,无法调用

Failed to execute ognl, exception message: ognl.MethodFailedException: Method "get" failed for object com.mcd.cn.bbl.c8s.redis.C8sRedisClient@32dcc2aa [java.lang.NullPointerException], please check $HOME/logs/arthas/arthas.log for more details. 用子类调用报没有这个方法

Failed to execute ognl, exception message: ognl.MethodFailedException: Method "getBean" failed for object org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2bbaf4f0, started on Fri Jan 21 15:35:55 GMT+08:00 2022 [org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'abstractRedisClient' available], please check $HOME/logs/arthas/arthas.log for more details. 用父类调用,报没有这个bean

期望的结果

可以调用

实际运行的结果

无法调用

回答

1

父类报错这个,是不是是调用getBean方法参数传错了?期望调用org.springframework.beans.factory.BeanFactory#getBean(java.lang.Class<T>),由于参数错传成String,变成了调用org.springframework.beans.factory.BeanFactory#getBean(java.lang.String)

1

父类报错这个,是不是调用getBean方法参数传了?org.springframework.beans.factory.BeanFactory#getBean(java.lang.Class<T>)``String``org.springframework.beans.factory.BeanFactory#getBean(java.lang.String)

不是的,下面的是完整的命令,因为父类不是bean,所以没有被注入,getbean自然就找不到了,但是通过子类应该可以调用父类的方法呀,现在调用子类,arthas报没有这个方法。。。 [arthas@6]$ognl -x 3 '#springContext=@xxxxxx.MyApplicationContext@context,#springContext.getBean("abstractRedisClient").get("xxx")' -c 60addb54 Failed to execute ognl, exception message: ognl.MethodFailedException: Method "getBean" failed for object org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2bbaf4f0, started on Thu Jan 27 17:09:05 GMT+08:00 2022 [org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'abstractRedisClient' available], please check $HOME/logs/arthas/arthas.log for more details.

[arthas@6]$ ognl -x 3 '#springContext=@xxxxxx.MyApplicationContext@context,#springContext.getBean(abstractRedisClient).get("xxx")' -c 60addb54 Failed to execute ognl, exception message: ognl.OgnlException: source is null for getProperty(null, "abstractRedisClient"), please check $HOME/logs/arthas/arthas.log for more details.

7

假设

public  abstract class AbstractRedisClient{
//......
}

@Compoment("myRedisClient")
public class MyRedisClient extends AbstractRedisClient{
//......
}

用ONGL调用getBean,传入myRedisClient或者AbstractRedisClient.class

2

假设

public  abstract class AbstractRedisClient{
//......
}

@Compoment("myRedisClient")
public class MyRedisClient extends AbstractRedisClient{
//......
}

用ONGL调用getBean,传入myRedisClient或者AbstractRedisClient.class

调用getBean,传入AbstractRedisClient.class的话会报没有这个bean,因为AbstractRedisClient.class确实不是bean,没有注入容器。 传入myRedisClient,会报没有这个方法,因为子类myRedisClient里没有这个方法,方法的实现在父类AbstractRedisClient.class里,死环。。。

5

子类bean如果注入容器的话,从容器中是可以通过父类class拿到的。

企业微信截图_caf04df5-4909-4605-bbd9-80cadae0ee23
4

看上面,你传入的是AbstractRedisClient,不是AbstractRedisClient.class.