Spring Boot provides access to all Spring-managed beans through the ApplicationContext. By retrieving the bean definition names from the ApplicationContext, developers can view every bean that has been created and registered in the Spring container during application startup.
Why Would We List All Beans?
Listing all beans helps achieve this.
How Does It Work?
Spring Boot Application
|
ApplicationContext
|
Stores All Beans
|
getBeanDefinitionNames()
|
Returns Bean Names
The ApplicationContext acts as the central container for all Spring-managed objects.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class BeanViewer {
@Autowired
private ApplicationContext applicationContext;
public void printBeans() {
String[] beans =
applicationContext
.getBeanDefinitionNames();
for (String bean : beans) {
System.out.println(bean);
}
}
}employeeController
employeeService
employeeRepository
dataSource
entityManagerFactory
dispatcherServletThese are some of the beans managed by Spring.
Displaying Beans at Startup
A common approach is to use CommandLineRunner.
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class BeanPrinter
implements CommandLineRunner {
private final ApplicationContext context;
public BeanPrinter(
ApplicationContext context) {
this.context = context;
}
@Override
public void run(String... args) {
String[] beans =
context.getBeanDefinitionNames();
for (String bean : beans) {
System.out.println(bean);
}
}
}When the application starts, all bean names are printed to the console.
Getting the Total Number of Beans
int count =
applicationContext
.getBeanDefinitionCount();
System.out.println(
"Total Beans: " + count);Total Beans: 250
The exact number depends on the application's dependencies and configurations.
Using ApplicationContext helps verify that all required beans are successfully loaded.
Alternative Using Actuator
/actuator/beans
without writing any custom code.
Benefits
Real-World Example
Suppose a Service bean is not being injected correctly.
This helps identify configuration problems much faster.
A concise interview answer is
We can retrieve all beans in a Spring Boot application by injecting the ApplicationContext and calling the getBeanDefinitionNames() method. This returns the names of all Spring-managed beans currently loaded in the container. We can also use the /actuator/beans endpoint if Spring Boot Actuator is enabled.
Last updated: 11 November 2026
Previous question: How do you enable Actuator in Spring Boot?
Next question: How can you check the environment properties of a Spring Boot application?