打印所有Spring boot加载的Bean类

时间:2020-02-23 14:35:42  来源:igfitidea点击:

在本教程中,我们将看到如何打印Spring启动加载的所有bean。

我们已经使用JSP创建了一个示例Spring Boot Hello World程序。

我们必须想知道Spring在背景中装满了 Bean类。

更改SpringboothellyorLDapplication.java如下:

package org.igi.theitroad;
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.context.ApplicationContext;
 
@SpringBootApplication
public class SpringBootHelloWorldApplication implements CommandLineRunner {
 
	@Autowired
	private ApplicationContext appContext;
 
	public static void main(String[] args) 
	{
		SpringApplication.run(SpringBootHelloWorldApplication.class, args);   
	}
 
	@Override
	public void run(String... arg0) throws Exception {
		String[] beans = appContext.getBeanDefinitionNames();
		Arrays.sort(beans);
		for (String bean : beans) {
			System.out.println(bean);
		}	
	}
}

我们只需要实现CommandLinerUnner并使用@Autowire注释获取应用程序窗口对象。

basicErrorController
beanNameHandlerMapping
beanNameViewResolver
characterEncodingFilter
conventionErrorViewResolver
defaultServletHandlerMapping
defaultValidator
defaultViewResolver
dispatcherServlet
dispatcherServletRegistration
duplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor
error
errorAttributes
errorPageCustomizer
errorPageRegistrarBeanPostProcessor
faviconHandlerMapping
faviconRequestHandler
handlerExceptionResolver
helloWorldController
hiddenHttpMethodFilter
httpPutFormContentFilter
httpRequestHandlerAdapter
HymansonObjectMapper
HymansonObjectMapperBuilder
jsonComponentModule
localeCharsetMappingsCustomizer
mappingHymanson2HttpMessageConverter
...
...