Spring Boot – How to get list of all loaded beans

In spring boot, we will see how to list all beans, how to get all beans, how to get list of beans, or a list of all loaded and available beans in the spring boot application context. During the application development in spring boot, we end up with an exception, because some of the beans are not loaded in the applicationcontext.

If you miss annotation in the bean class, it will not load into context. To understand what beans are available in context memory at this point, you can use this example.

When the spring boot application starts, the required boot class, configuration classes, annotation classes, jmx classes and scheduler classes are loaded. Then custom classes are loaded into the memory with dependency injection and annotation.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.yawintutor</groupId>
	<artifactId>SpringBootLoadedBean</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootLoadedBean</name>
	<description>Spring Boot Cache Project</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

SpringBootLoadedBeanApplication.java

package com.yawintutor.bean;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringBootLoadedBeanApplication {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringBootLoadedBeanApplication.class, args);
		
		for(String bean:context.getBeanDefinitionNames()) {
			System.out.println(bean);
		}
	}

}

In the example above, the ApplicationContext variable is created from the main method. We can also use @Autowire annotation to get ApplicationContext handle. In the ApplicationContext, the api getBeanDefinitionNames () contains a list of all bean names loaded. All loaded beans are printed by iterating the list in this example.

The project file structure and files are shown in the picture below.

The output appears in the console window. The list of all loaded beans is shown in the image below.



List of loaded beans

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springBootLoadedBeanApplication
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.condition.BeanTypeRegistry
propertySourcesPlaceholderConfigurer
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
mbeanExporter
objectNamingStrategy
mbeanServer
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration
springApplicationAdminRegistrar
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
taskExecutorBuilder
applicationTaskExecutor
spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
taskSchedulerBuilder
spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *