You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
arthas/tutorials/katacoda/arthas-advanced-cn/case-get-spring-context.md

55 lines
2.0 KiB
Markdown

在这个案例里展示获取spring context再获取bean然后调用函数。
### 使用tt命令获取到spring context
`tt`即 TimeTunnel它可以记录下指定方法每次调用的入参和返回信息并能对这些不同的时间下调用进行观测。
* https://arthas.aliyun.com/doc/tt.html
`tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod`{{execute T2}}
访问https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/1
可以看到`tt`命令捕获到了一个请求:
```bash
$ tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdaptePress Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 252 ms.
INDE TIMESTAMP COST( IS-R IS- OBJECT CLASS METHOD
X ms) ET EXP
-----------------------------------------------------------------------------------------
1000 2019-02-15 4.583 true fal 0xc93cf1a RequestMappingHand invokeHandlerMethod
15:38:32 923 se lerAdapter
```
### 使用tt命令从调用记录里获取到spring context
输入 `Q`{{execute T2}} 或者 `Ctrl + C` 退出上面的 `tt -t`命令。
`tt -i 1000 -w 'target.getApplicationContext()'`{{execute T2}}
```bash
$ tt -i 1000 -w 'target.getApplicationContext()'
@AnnotationConfigEmbeddedWebApplicationContext[
reader=@AnnotatedBeanDefinitionReader[org.springframework.context.annotation.AnnotatedBeanDefinitionReader@2e457641],
scanner=@ClassPathBeanDefinitionScanner[org.springframework.context.annotation.ClassPathBeanDefinitionScanner@6eb38026],
annotatedClasses=null,
basePackages=null,
]
Affect(row-cnt:1) cost in 439 ms.
```
## 获取spring bean并调用函数
`tt -i 1000 -w 'target.getApplicationContext().getBean("helloWorldService").getHelloMessage()'`{{execute T2}}
结果是:
```bash
$ tt -i 1000 -w 'target.getApplicationContext().getBean("helloWorldService").getHelloMessage()'
@String[Hello World]
Affect(row-cnt:1) cost in 52 ms.
```