🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Spring Boot – 获取所有已加载的带有类类型信息的 bean > 原文: [https://howtodoinjava.com/spring-boot/get-loaded-beans-class-type-in​​formation/](https://howtodoinjava.com/spring-boot/get-loaded-beans-class-type-information/) Spring Boot 在内部加载了大量的 bean,以最小的配置运行您的应用程序。 在此示例中,我们将学习找出所有这些 **SpringBoot 加载的 Bean** 及其类类型信息。 ## 使用`ApplicationContext`获取所有已加载的 bean 若要自动执行方法,请在应用程序完全加载后使用`CommandLineRunner`接口。[`CommandLineRunner`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/CommandLineRunner.html)用于指示当 bean 包含在 Spring 应用程序中时,它应该**运行**。 1)使用`ApplicationContext.getBeanDefinitionNames()`查找所有已加载 bean 的名称 2)使用`ApplicationContext.getBean(beanName)`获取包含其运行时类型信息的 bean。 ```java 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()); } } } ``` 运行上面的应用程序将在控制台中打印 bean 名称和类型信息,如下所示: ```java 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 ... ... ... ``` 我已经截断了输出。 您可以自己验证整个列表。 学习愉快!