arthas获取spring bean

2023-12-17 14:35:05

参考文章

arthas获取spring bean

写一个工具Util

package com.example.lredisson.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author lixi
    ognl -c 18b4aac2 '#context=@com.example.lredisson.util.OSSSpringUtil@getContext(),#context.getBean("testModel").getAge()'
 * 
 */

@Component
public class OSSSpringUtil implements ApplicationContextAware {
    private static ApplicationContext context;


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

    public static <T> T getBean(String beanName, Class<T> clazz) {
        return context.getBean(beanName, clazz);
    }

    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

    public static ApplicationContext getContext() {
        return context;
    }
}

结果

sc -d com.example.lredisson.util.OSSSpringUtil
 class-info        com.example.lredisson.util.OSSSpringUtil                                                                       
 code-source       /Users/admin/work/llixi/lredisson/target/classes/                                                              
 name              com.example.lredisson.util.OSSSpringUtil                                                                       
 isInterface       false                                                                                                          
 isAnnotation      false                                                                                                          
 isEnum            false                                                                                                          
 isAnonymousClass  false                                                                                                          
 isArray           false                                                                                                          
 isLocalClass      false                                                                                                          
 isMemberClass     false                                                                                                          
 isPrimitive       false                                                                                                          
 isSynthetic       false                                                                                                          
 simple-name       OSSSpringUtil                                                                                                  
 modifier          public                                                                                                         
 annotation        org.springframework.stereotype.Component                                                                       
 interfaces        org.springframework.context.ApplicationContextAware                                                            
 super-class       +-java.lang.Object                                                                                             
 class-loader      +-sun.misc.Launcher$AppClassLoader@18b4aac2                                                                    
                     +-sun.misc.Launcher$ExtClassLoader@6aceb1a5                                                                  
 classLoaderHash   18b4aac2                                                                                                       

Affect(row-cnt:1) cost in 28 ms.
[arthas@12906]$ ognl -c 18b4aac2 '#context=@com.example.lredisson.util.OSSSpringUtil@getContext(),#context.getBean("testModel").getAge()'
@Integer[20]

在这里插入图片描述

arthas验证先生代码版本

直接找到需要判断的class,用jad反编译看看结果最简单了啊。

文章来源:https://blog.csdn.net/xiaolixi199311/article/details/135044238
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。