In this post, we will see how to print all the beans loaded by Spring boot.
We have already created a sample spring boot hello world program using JSP. You must be wondering what beans have been loaded by Spring in background.
Change SpringBootHelloWorldApplication.java as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; 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); } } } |
You just need to implement CommandLineRunner and get ApplicatationContext object using @Autowire annotation.
beanNameHandlerMapping
beanNameViewResolver
characterEncodingFilter
conventionErrorViewResolver
defaultServletHandlerMapping
defaultValidator
defaultViewResolver
dispatcherServlet
dispatcherServletRegistration
duplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor
error
errorAttributes
errorPageCustomizer
errorPageRegistrarBeanPostProcessor
faviconHandlerMapping
faviconRequestHandler
handlerExceptionResolver
helloWorldController
hiddenHttpMethodFilter
httpPutFormContentFilter
httpRequestHandlerAdapter
jacksonObjectMapper
jacksonObjectMapperBuilder
jsonComponentModule
localeCharsetMappingsCustomizer
mappingJackson2HttpMessageConverter
…
…
That’s all about how to print all the beans loaded by Spring boot