arthas 获取springmvc子容器

本文介绍了如何在Spring MVC中通过`ContextLoader.getCurrentWebApplicationContext()`获取Spring容器,以及如何利用`getServletContext()`获取Servlet上下文对象。讲解了Spring MVC子容器在servletContext的attribute中的存储方式,并提供了OGNL表达式来遍历和获取子容器。此外,还提到了在非Spring Boot项目中使用`vmtool`工具来查找和操作特定类实例的方法,包括清除缓存等操作。对于Spring Boot项目,上述方法不适用。

org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext(): 获取的是spring容器

org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext().getServletContext():获取的是servletContext对象

springmvc子容器存储在servletContext的attribute中,key为:org.springframework.web.servlet.FrameworkServlet.SERVLET_CONTEXT_PREFIX + servlet名称

不确定名称为何可以将所有attribute打印出来(需要-c指明classloader的hash值):

ognl -c 567eb32e '@org.apache.commons.collections.EnumerationUtils@toList(@org.springframework.web.context.ContextLoader@getCurrentWebApplicationContext().getServletContext().getAttributeNames())'

获取springmvc子容器:

ognl -c 567eb32e '@org.springframework.web.context.support.WebApplicationContextUtils@getWebApplicationContext(@org.springframework.web.context.ContextLoader@getCurrentWebApplicationContext().getServletContext(), "org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring")'


上面方法不适用于springboot项目

所有项目适用的方式vmtool

vmtool -x 1 --action getInstances --className com.xxx.service.impl.XXXXServiceImpl -c 5ffd2b27 --limit 3

-x 1 展开层数,查找类实例时,展开1层即可,可以看到实例类名

--limit 3 最多展示多少个,如果不会特别多可以不限制,默认limit 10个

找到对应实例后,使用实例执行方法

vmtool -x 1 --action getInstances --className com.xxx.service.impl.XXXXServiceImpl -c 5ffd2b27 --express 'instances[1].timedCache.clear()'

--express 执行表达式内的值,instances[1]表示使用实例的第几个,是一个数组,第一个下标为0

参考vmtool | arthas 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值