前言
Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息;
通过ApplicationContext 去获取所有的Bean
通过CommandLineRunner
接口,可以实现在Spring Boot完全启动后执行一些代码逻辑,本文将执行的逻辑是打印所有Bean的信息;
ApplicationContext.getBeanDefinitionNames()
方法获取所有Bean的名称; 2) 通过 ApplicationContext.getBean(beanName)
获取Bean的详细信息; 具体代码实现如下:
package com.howtodoinjava.app.controller; import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer;import org.springframework.context.ApplicationContext; @SpringBootApplicationpublic class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } @Autowired private ApplicationContext appContext; @Override public void run(String... args) throws Exception { String[] beans = appContext.getBeanDefinitionNames(); Arrays.sort(beans); for (String bean : beans) { System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass()); } }}
运行以上程序,控制台将打印如下信息:
2017-03-06 13:22:50 - Tomcat started on port(s): 8080 (http) basicErrorController of Type :: class org.springframework.boot.autoconfigure.web.BasicErrorControllerbeanNameHandlerMapping of Type :: class org.springframework.web.servlet.handler.BeanNameUrlHandlerMappingbeanNameViewResolver of Type :: class org.springframework.web.servlet.view.BeanNameViewResolvercharacterEncodingFilter of Type :: class org.springframework.boot.web.filter.OrderedCharacterEncodingFilterconventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorViewResolverdefaultServletHandlerMapping of Type :: class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMappingdefaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolverdispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServletdispatcherServletRegistration of Type :: class org.springframework.boot.web.servlet.ServletRegistrationBeanduplicateServerPropertiesDetector of Type :: class org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration$DuplicateServerPropertiesDetectorembeddedServletContainerCustomizerBeanPostProcessor of Type :: class org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessorerror of Type :: class org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelViewerrorAttributes of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorAttributes.........