Spring Boot - 获取所有的Bean信息
2018-10-14 12:29
417 查看
前言
Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息;
通过ApplicationContext 去获取所有的Bean
通过
CommandLineRunner接口,可以实现在Spring Boot完全启动后执行一些代码逻辑,本文将执行的逻辑是打印所有Bean的信息;
1) 通过
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; @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 ... ... ...
相关文章推荐
- 关于SpringBoot获取IOC容器中注入的Bean(推荐)
- 70.打印所有Spring boot载入的bean【从零开始学Spring Boot】
- springboot kafka消费者获取不到信息
- Spring 获取 实现某接口的所有实例bean
- 单元测试之获取Spring下所有Bean
- spring boot获取注入的实例bean
- SpringBoot文件上传控制及Java 获取和判断文件头信息
- Spring启动后获取所有拥有特定注解的Bean实例代码
- 在springboot项目中service层获取当前用户信息
- 使用spring-boot-starter-actuator http://localhost:8080/info无法获取到相应信息
- 70. 打印所有Spring boot载入的bean
- 通过ApplicationContextAware接口获取Spring配置文件中的所有Bean
- Spring Boot 微信-网页授权获取用户信息
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- 11-Spring Boot ( 获取配置文件信息 )
- 在SpringBoot中获取某个注解标记的`BeanName`
- SpringBoot普通类中如何获取其他bean例如Service、Dao
- spring boot +security获取当前登录用户信息
- Springboot中随时获取ApplicationContext,得到想要的bean
- spring boot如何在所有bean初始化之前加载一个自定义类?