Get all Loaded Beans in Spring Boot

Spread the love

Spring boot loads lots of beans internally to run your application with minimal configuration. In this example, we will learn to find out all those spring boot loaded beans and their class type information.

Using ApplicationContext to get all loaded beans

To execute a method automatically, when the application is fully loaded, I am using CommandLineRunner interface. CommandLineRunner is used to indicate that a bean should run when it is contained within a Spring Application.

1) Use ApplicationContext.getBeanDefinitionNames() to find the name of all loaded beans
2) Use ApplicationContext.getBean(beanName) to get bean including its runtime type information.

package com.example.demo;

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.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;

import java.util.Arrays;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer implements CommandLineRunner {

	@Autowired
	private ApplicationContext appContext;

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(DemoApplication.class);
	}

	@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());
		}
	}
}

Running above application will print bean names and type information in the console like below:

2020-02-23 13:35:19.166  INFO 13304 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.992 seconds (JVM running for 2.509)
applicationTaskExecutor of Type :: class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
basicErrorController of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.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.servlet.filter.OrderedCharacterEncodingFilter
conventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver
defaultServletHandlerMapping of Type :: class org.springframework.beans.factory.support.NullBean
defaultValidator of Type :: class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
defaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolver
demoApplication of Type :: class com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a60f37ef
dispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServlet
dispatcherServletRegistration of Type :: class org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean
error of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$StaticView
errorAttributes of Type :: class org.springframework.boot.web.servlet.error.DefaultErrorAttributes
errorPageCustomizer of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$ErrorPageCustomizer
errorPageRegistrarBeanPostProcessor of Type :: class org.springframework.boot.web.server.ErrorPageRegistrarBeanPostProcessor
formContentFilter of Type :: class org.springframework.boot.web.servlet.filter.OrderedFormContentFilter
handlerExceptionResolver of Type :: class org.springframework.web.servlet.handler.HandlerExceptionResolverComposite
handlerFunctionAdapter of Type :: class org.springframework.web.servlet.function.support.HandlerFunctionAdapter
homeController of Type :: class com.example.demo.HomeController
httpRequestHandlerAdapter of Type :: class org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
jacksonObjectMapper of Type :: class com.fasterxml.jackson.databind.ObjectMapper
jacksonObjectMapperBuilder of Type :: class org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
jsonComponentModule of Type :: class org.springframework.boot.jackson.JsonComponentModule
localeCharsetMappingsCustomizer of Type :: class org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration$LocaleCharsetMappingsCustomizer
mappingJackson2HttpMessageConverter of Type :: class org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
messageConverters of Type :: class org.springframework.boot.autoconfigure.http.HttpMessageConverters
methodValidationPostProcessor of Type :: class org.springframework.validation.beanvalidation.MethodValidationPostProcessor
multipartConfigElement of Type :: class javax.servlet.MultipartConfigElement
multipartResolver of Type :: class org.springframework.web.multipart.support.StandardServletMultipartResolver
mvcContentNegotiationManager of Type :: class org.springframework.web.accept.ContentNegotiationManager
mvcConversionService of Type :: class org.springframework.boot.autoconfigure.web.format.WebConversionService
mvcHandlerMappingIntrospector of Type :: class org.springframework.web.servlet.handler.HandlerMappingIntrospector
mvcPathMatcher of Type :: class org.springframework.util.AntPathMatcher
mvcResourceUrlProvider of Type :: class org.springframework.web.servlet.resource.ResourceUrlProvider
mvcUriComponentsContributor of Type :: class org.springframework.web.method.support.CompositeUriComponentsContributor
mvcUrlPathHelper of Type :: class org.springframework.web.util.UrlPathHelper
mvcValidator of Type :: class org.springframework.boot.autoconfigure.validation.ValidatorAdapter
mvcViewResolver of Type :: class org.springframework.web.servlet.view.ViewResolverComposite
org.springframework.aop.config.internalAutoProxyCreator of Type :: class org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator
org.springframework.boot.autoconfigure.AutoConfigurationPackages of Type :: class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration of Type :: class org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration of Type :: class org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration
org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration of Type :: class org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration
org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration of Type :: class org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory of Type :: class org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration of Type :: class org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat of Type :: class org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter of Type :: class org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration of Type :: class org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration of Type :: class org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration
org.springframework.boot.context.internalConfigurationPropertiesBinder of Type :: class org.springframework.boot.context.properties.ConfigurationPropertiesBinder
org.springframework.boot.context.internalConfigurationPropertiesBinderFactory of Type :: class org.springframework.boot.context.properties.ConfigurationPropertiesBinder$Factory
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata of Type :: class org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator of Type :: class org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor of Type :: class org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor of Type :: class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor of Type :: class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
org.springframework.context.annotation.internalConfigurationAnnotationProcessor of Type :: class org.springframework.context.annotation.ConfigurationClassPostProcessor
org.springframework.context.event.internalEventListenerFactory of Type :: class org.springframework.context.event.DefaultEventListenerFactory
org.springframework.context.event.internalEventListenerProcessor of Type :: class org.springframework.context.event.EventListenerMethodProcessor
parameterNamesModule of Type :: class com.fasterxml.jackson.module.paramnames.ParameterNamesModule
preserveErrorControllerTargetClassPostProcessor of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$PreserveErrorControllerTargetClassPostProcessor
propertySourcesPlaceholderConfigurer of Type :: class org.springframework.context.support.PropertySourcesPlaceholderConfigurer
requestContextFilter of Type :: class org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter
requestMappingHandlerAdapter of Type :: class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
requestMappingHandlerMapping of Type :: class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
resourceHandlerMapping of Type :: class org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
restTemplateBuilder of Type :: class org.springframework.boot.web.client.RestTemplateBuilder
routerFunctionMapping of Type :: class org.springframework.web.servlet.function.support.RouterFunctionMapping
server-org.springframework.boot.autoconfigure.web.ServerProperties of Type :: class org.springframework.boot.autoconfigure.web.ServerProperties
servletWebServerFactoryCustomizer of Type :: class org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer
simpleControllerHandlerAdapter of Type :: class org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
spring.http-org.springframework.boot.autoconfigure.http.HttpProperties of Type :: class org.springframework.boot.autoconfigure.http.HttpProperties
spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties of Type :: class org.springframework.boot.autoconfigure.info.ProjectInfoProperties
spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonProperties
spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties of Type :: class org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties
spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties of Type :: class org.springframework.boot.autoconfigure.web.ResourceProperties
spring.security.oauth2.resourceserver-org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties of Type :: class org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties
spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties of Type :: class org.springframework.boot.autoconfigure.web.servlet.MultipartProperties
spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties of Type :: class org.springframework.boot.autoconfigure.task.TaskExecutionProperties
spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties of Type :: class org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
standardJacksonObjectMapperBuilderCustomizer of Type :: class org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration$StandardJackson2ObjectMapperBuilderCustomizer
stringHttpMessageConverter of Type :: class org.springframework.http.converter.StringHttpMessageConverter
taskExecutorBuilder of Type :: class org.springframework.boot.task.TaskExecutorBuilder
taskSchedulerBuilder of Type :: class org.springframework.boot.task.TaskSchedulerBuilder
tomcatServletWebServerFactory of Type :: class org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
tomcatServletWebServerFactoryCustomizer of Type :: class org.springframework.boot.autoconfigure.web.servlet.TomcatServletWebServerFactoryCustomizer
tomcatWebServerFactoryCustomizer of Type :: class org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer
viewControllerHandlerMapping of Type :: class org.springframework.beans.factory.support.NullBean
viewResolver of Type :: class org.springframework.web.servlet.view.ContentNegotiatingViewResolver
webServerFactoryCustomizerBeanPostProcessor of Type :: class org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor
websocketServletWebServerCustomizer of Type :: class org.springframework.boot.autoconfigure.websocket.servlet.TomcatWebSocketServletWebServerCustomizer
welcomePageHandlerMapping of Type :: class org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping

Leave a Reply