Arthas查看SpringBoot配置及ognl-ClassNotFoundException处理

👽个人博客:https://everspring.github.io/
👽公众号:爱历史的IT男

能看到这篇文章的同学大概率已经知道Arthas是什么了,这里就不多余赘述,本文介绍一下通过Arthas查看SpringBoot工程的配置及曾遇到过的问题。
本文相关知识:scognlspring配置保存的地方

注意:ognl是3.5.0以后出现的

  1. arthas有可以查看JVM环境变量的sysenv,也有可以查看和修改JVM的系统属性的sysprop,但SpringBoot配置文件的内容不是在环境变量和系统属性中的,而是在ConfigurableApplicationContextenvironmentConfigurableApplicationContext继承自ApplicationContext,在我们的工程中经常要用到ApplicationContext,可以从ApplicationContext入手拿到environment。
  2. 通过ognl表达式获取,ognl在java中运用广泛,比如Spring Cache的注解、Spring @Value注解都有用到。假如工程下有一个com.xxx.spring.util.SpringUtils(见文末)可以获取到ApplicationContext,想要获取到application.yml中在server.port的值,可以采用如下方法
ognl '#context=@com.xxx.util.SpringUtils@applicationContext,#context.getEnvironment().getProperty("server.port")'

#context --代表的变量
@com.xxx.util.SpringUtils --指定的类
@applicationContext --类的属性
#context.getEnvironment().getProperty(“server.port”) --通过context获取到environment属性,然后再通过environment获取到property的值

  1. 有些情况下通过ognl获取applicationContext的时候会出现ClassNotFoundException,详情如下:

Failed to execute ognl, exception message: ognl.OgnlException: Could not get static field applicationContext from class com.xxx.util.SpringUtils [java.lang.ClassNotFoundException: Unable to resolve class: com.xxx.util.SpringUtils], please check $HOME/logs/arthas/arthas.log for more details.

出现此类问题是由于classloader不是默认加载器导致,见官网issue:https://github.com/alibaba/arthas/issues/71
解决方法:

  • 1.通过sc -d 查询出当前类的classloader
sc -d com.xxx.util.SpringUtils

image-20220327234537394

获取到classLoaderHash值

  • 2.使用指定的classloader。ognl -c classloader的hash值
ognl '#context=@com.xxx.common.utils.spring.SpringUtils@applicationContext,#context.getEnvironment().getProperty("dev.name")' -c 20ad941

SpringUtils的代码

@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringUtils.applicationContext = applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
        return (T) beanFactory.getBean(name);
    }
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值