java怎么看程序成员变量_基于Spring 使用Arthas查看成员变量值

如何使用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 getBean(String beanName) {

assertApplicationContext();

return (T) applicationContext.getBean(beanName);

}

public static  T getBean(Class 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> 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 成员变量的值。

bc7fdfc03a181785f939cc7c3cabcadc.gif[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值