本文主要是介绍Spring Boot - 使用类类型信息获取所有已加载的bean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring启动会在内部加载大量bean,以最少的配置运行您的应用程序。在这个例子中,我们将学习如何找出所有那些Spring boot加载的bean及其类类型信息。
使用ApplicationContext获取所有已加载的bean
要自动执行方法,当应用程序完全加载时,我正在使用CommandLineRunner
接口。CommandLineRunner用于指示bean 在Spring应用程序中包含时应该运行。
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()); } } } |
在应用程序上运行将在控制台中打印bean名称和类型信息,如下所示:
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 ... ... ... |
我截断了输出。您可以自己验证整个列表。
这篇关于Spring Boot - 使用类类型信息获取所有已加载的bean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!