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





