有没有办法打印启动时加载的所有spring bean?我使用的是Spring2.0。
发布于 2019-11-28 10:41:52
下面是打印来自spring应用程序上下文的所有bean名称的另一种方法:
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