如何使用arthas查看成员变量
搜了一下官方文档没找到合适方法直接查看对象的成员变量,发现一位朋友有这样一个办法,一般我们用Spring时,可以写一个Holder类,实现ApplicationContextWare接口,这样我们在Spring加载后可以获取其context变量,context.getBean可以通过类名获取对象。所以基于这个想法,可以这样写,完整示例如下:
1、定义Holder类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }
    public static <T> T getBean(String beanName) {
        assertApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }
    public static <T> T getBean(Class<T> requiredType) {
        assertApplicationContext();
        return applicationContext.getBean(requiredType);
    }
    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了SpringContextHolder!");
        }
    }
}2、测试服务
import com.alibaba.fastjson.JSON;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@Component
public class DocService {
    private int id = 1;
    private String name = "程序喵";
    private Cache<String, Optional<String>> cache = CacheBuilder.newBuilder()
            .expireAfterWrite(30, TimeUnit.MINUTES).build();
    public String doSomething() {
        // 加入缓存
        cache.put("tingfeng", Optional.ofNullable("单身,求撩"));
        return id + " : " + name;
    }
}3、测试入口
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private DocService service;
    @GetMapping("/test")
    public Object test() {
        return service.doSomething();
    }
}4、开始 arthas 测试
我想要观察到 DocService 下的 id,name,cache 成员变量的值。
[arthas@89254]$ ognl '@com.esper.utils.SpringContextHolder@getBean("docService").id'
@Integer[1]
[arthas@89254]$ ognl '@com.esper.utils.SpringContextHolder@getBean("docService").name'
@String[程序喵]
[arthas@89254]$ ognl '@com.esper.utils.SpringContextHolder@getBean("docService").cache'
@LocalManualCache[
    localCache=@LocalCache[isEmpty=true;size=0],
    serialVersionUID=@Long[1],
]开始执行 cache 时没有值,调用一下接口给 cache 赋值,http://localhost:8080/index/test
[arthas@89254]$ ognl '@com.esper.utils.SpringContextHolder@getBean("docService").cache'
@LocalManualCache[
    localCache=@LocalCache[isEmpty=false;size=1],
    serialVersionUID=@Long[1],
]
[arthas@89254]$ ognl '@com.esper.utils.SpringContextHolder@getBean("docService").cache.asMap()'
@LocalCache[
    @String[tingfeng]:@Optional[Optional[单身,求撩]],
]
[arthas@89254]$另外 arthas 提供了查看静态变量的方式
- 查看运行的代码:jad com.xx.ClassName 
- 查看类的静态成员:getstatic cn.xx.ClassName propName 
- 查看方法:jad cn.xx.ClassName toString 
未经允许请勿转载:程序喵 » 基于Spring 使用Arthas查看成员变量值
 程序喵
程序喵