Print all the Spring beans that are loaded

前端 未结 8 1754
说谎
说谎 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:51

    Here is another way to print all the bean names from the spring application context:

    import java.util.Arrays;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    /***********************************************************************************************************
     * Java File: MainApplication.java
     * Description: Main class to run the application.
     * 
     ***********************************************************************************************************/
    
    @SpringBootApplication
    public class MainApplication {
    
    private static final Logger logger = LogManager.getLogger(MainApplication.class);
    
    public static void main(String[] args) 
    {
        final ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
    
        final AtomicInteger counter = new AtomicInteger(0);
        logger.info("**************** START: Total Bean Objects: {} ******************", context.getBeanDefinitionCount());
    
        Arrays.asList(context.getBeanDefinitionNames())
        .forEach(beanName -> {
            logger.info("{}) Bean Name: {} ", counter.incrementAndGet(), beanName);
        });
    
        logger.info("**************** END: Total Bean: {} ******************", context.getBeanDefinitionCount());
    }
    
    }
    
    
    Sample Output:
    
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:18] - **************** START: Total Bean Objects: 564 ****************** 
    ...........................
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:22] - 460) Bean Name: mvcPathMatcher  
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:22] - 461) Bean Name: mvcUrlPathHelper  
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:22] - 462) Bean Name: viewControllerHandlerMapping  
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:22] - 463) Bean Name: beanNameHandlerMapping  
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:22] - 464) Bean Name: resourceHandlerMapping 
    ...........................
    2019-11-27 20:08:02.821 INFO  [main] [c.c.a.MainApplication:25] - **************** END: Total Bean: 564 ****************** 
    

提交回复
热议问题