Print all the Spring beans that are loaded

前端 未结 8 1756
说谎
说谎 2020-11-28 03:05

Is there a way to print all the spring beans that are loaded on startup?I am using Spring 2.0.

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 03:33

    Using spring-boot-starter-actuator you can easily access all bean.

    Here is the setup process:

    1. Add dependency into gradle:

    Add bellow into gradle file:

    compile("org.springframework.boot:spring-boot-starter-actuator")
    
    1. Enable security on application.properties:

    Add management.security.enabled=false into your application.property file

    1. call /beans endpoint:

      After that setup spring will enable some metrics related endpoints. One of its endpoint is /beans After calling this endpoints it will provide a json file that contains all of your bean including it's dependency and scope.

    Here is an example json file:

    [{"context":"application:8442","parent":null,"beans":[{"bean":"beanName","aliases":[],"scope":"singleton","type":"packageName$$4b46c703","resource":"null","dependencies":["environment","beanName1","beanName2"]},{"bean":"org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory","aliases":[],"scope":"singleton","type":"org.springframework.core.type.classreading.CachingMetadataReaderFactory","resource":"null","dependencies":[]}]
    

    For more info visit bellow links:

    • Building a RESTful Web Service with Spring Boot Actuator
    • Spring Boot Actuator: Health check, Auditing, Metrics gathering
    • Spring Boot Actuator: Production-ready features

    Hope this will help you. Thanks :)

提交回复
热议问题