深入理解Spring Cloud与微服务构建(第2版)
上QQ阅读APP看书,第一时间看更新

4.4.2 查看运行程序的Bean

如果需要了解Spring Boot上下文注入了哪些Bean,这些Bean的类型、状态、生命周期等信息时,只需要发起一个GET类型的请求,请求API为“/beans”,在浏览器上访问 “http://locahost:9001/actuator/beans”,浏览器会显示如下的信息:

 [
    {
        "context": "application:dev:8082",
        "parent": null,
        "beans": [
           {
              "bean": "springbootFirstApplication",
              "aliases": [
              ],
              "scope": "singleton",
              "type": "com.forezp.SpringbootFirstApplication$$EnhancerBySpringCGLIB$$3efbe85a",
              "resource": "null",
              "dependencies": [
              ]
           },
           {
              "bean": "org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory",
              "aliases": [
              ],
              "scope": "singleton",
              "type": "org.springframework.core.type.classreading.CachingMetadataReaderFactory",
              "resource": "null",
              "dependencies": [
              ]
           },
           {
              "bean": "configBean",
              "aliases": [
              ],
              "scope": "singleton",
              "type": "com.forezp.bean.ConfigBean",
              "resource": "file [F:/book/springboot-first-application/target/classes/com/forezp/bean/ConfigBean.class]",
              "dependencies": [
              ]
           },
           {
              "bean": "user",
              "aliases": [
              ],
              "scope": "singleton",
              "type": "com.forezp.bean.User$$EnhancerBySpringCGLIB$$eb8cd986",
              "resource": "file [F:/book/springboot-first-application/target/classes/com/forezp/bean/User.class]",
              "dependencies": [
              ]
           },
           {
              "bean": "helloController",
              "aliases": [
              ],
              "scope": "singleton",
              "type": "com.forezp.web.HelloController",
              "resource": "file [F:/book/springboot-first-application/target/classes/com/forezp/web/HelloController.class]",
              "dependencies": [
              ]
           },
           {
              "bean": "lucyController",
              "aliases": [
              ],
              "scope": "singleton",
              "type": "com.forezp.web.LucyController",
              "resource": "file [F:/book/springboot-first-application/target/classes/com/forezp/web/LucyController.class]",
              "dependencies": [
                  "configBean",
                  "user"
              ]
           }
          ...
       ]

在返回的信息中包含了Bean的以下4类信息。

•bean:Spring应用程序上下文中的Bean名称或Id。

•resource:class文件的物理位置,通常是一个Url,指向构建出的Jar文件的路径。

•scope:Bean的作用域(通常是单例singleton,也可以是ptototype、request和session)。

•type:Bean的类型。