Reproduced - to get through all the ApplicationContext Bean

Spring Boot - Get all the information Bean 
read the catalog 
foreword 
to get through all the ApplicationContext Bean 
preface 
Spring Boot boot time to load a lot of Bean minimized configuration, this article will try to find all the information loaded after the Spring Bean started; 

by ApplicationContext go All Bean acquired 
by CommandLineRunner interface logic may be implemented to execute some code in the Spring boot completely started, this logic is performed for all print information in Bean;
 1 ) Get the name through all Bean ApplicationContext.getBeanDefinitionNames () method;
 2 ) obtained by ApplicationContext.getBean (beanName) Bean details; a 

specific code is achieved as follows: 

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;
 
@SpringBootApplication
public 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.BasicErrorController
beanNameHandlerMapping of Type :: class org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
beanNameViewResolver of Type :: class org.springframework.web.servlet.view.BeanNameViewResolver
characterEncodingFilter of Type :: class org.springframework.boot.web.filter.OrderedCharacterEncodingFilter
conventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorViewResolver
defaultServletHandlerMapping of Type :: class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
defaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolver
dispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServlet
dispatcherServletRegistration of Type :: class org.springframework.boot.web.servlet.ServletRegistrationBean
duplicateServerPropertiesDetector of Type :: class org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration$DuplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor of Type :: class org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor
error of Type :: class org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView
errorAttributes of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorAttributes
...
...
...

@Author      风一样的码农
@HomePageUrl http://www.cnblogs.com/chenpi/ 
@Copyright please indicate the source, thank you ~ 

 

Guess you like

Origin www.cnblogs.com/xiaoshahai/p/11844112.html