获得ApplicationContext为所欲为
通过Arthas获得ApplicationContext的方法有多种,在特定的项目中使用适合的方式实现。
考虑通过vmtool实现,vmtool可以查询内存对象,通过这个特点, 可以直接指定类名称获得其实例列表,选取合适的实例即可。
bash
# 获得ApplicationContext
vmtool -a getInstances --className org.springframework.context.ApplicationContext \
--express "#ctx=instances[0], #bean=#ctx.getBean(@com.xxx.xxx.XxxBean@class),#bean.doSomething()"
# 既然都能获得ApplicationContext 我直接获取目标对象不香嘛?
[arthas@3716]$ vmtool -a getInstances --className com.xxx.xxx.system.application.service.UserService
@UserService[][
@UserService$$EnhancerBySpringCGLIB$$5af3ff3a[com.xxx.xxx.system.application.service.UserService@49f20b8c],
@UserService[com.xxx.xxx.system.application.service.UserService@49f20b8c],
]
# 拿到代理对象为所欲为
[arthas@3716]$ vmtool -a getInstances --className com.xxx.xxx.system.application.service.UserServic \
--express "instances[0].doSomething()"
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13