Print all Beans in Spring Boot at startup
During Spring Boot
application startup, many beans are created, some by Spring Boot
framework while some that
are configured for application. In case you need to print all the beans created for dubugging purpose, you can use
ConfigurableApplicationContext getBeanDefinitionNames()
method.
Below example shows how to get and print all Beans in a Spring Boot Application at startup.
Step 1) Create pom.xml and add below maven dependency
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
Step 2) Create PrintBeansApplication class
This Class prints name of all beans provided by Spring Boot and also the beans count.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class PrintBeansApplication { public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(PrintBeansApplication.class, args); System.out.println("Number of beans --> " + ctx.getBeanDefinitionCount()); String[] beanNames = ctx.getBeanDefinitionNames(); // get beans for (String beanName : beanNames) { System.out.println("Bean --> "+ beanName); // print bean } } }
Step 3) Running PrintBeansApplication
When you run PrintBeansApplication, you will see the list of beans printed in the console as shown below.
Output :
Number of beans --> 27 Bean --> org.springframework.context.annotation.internalConfigurationAnnotationProcessor Bean --> org.springframework.context.annotation.internalAutowiredAnnotationProcessor Bean --> org.springframework.context.annotation.internalCommonAnnotationProcessor Bean --> org.springframework.context.event.internalEventListenerProcessor Bean --> org.springframework.context.event.internalEventListenerFactory Bean --> printBeansApplication Bean --> org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory Bean --> org.springframework.boot.autoconfigure.AutoConfigurationPackages Bean --> org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration Bean --> org.springframework.boot.autoconfigure.condition.BeanTypeRegistry Bean --> propertySourcesPlaceholderConfigurer Bean --> org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration Bean --> mbeanExporter Bean --> objectNamingStrategy Bean --> mbeanServer Bean --> org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration Bean --> org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor Bean --> org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata Bean --> org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration Bean --> spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties Bean --> org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration Bean --> taskExecutorBuilder Bean --> applicationTaskExecutor Bean --> spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties Bean --> org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration Bean --> taskSchedulerBuilder Bean --> spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
References :
Spring Beans