How to get the list of all the beans in our Spring Boot application?

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.

Key Points: • ApplicationContext maintains all Spring-managed beans. • The getBeanDefinitionNames() method returns the names of all registered beans. • Useful for debugging, learning Spring internals, and troubleshooting bean-related issues. • Helps verify whether a bean has been successfully created and loaded. • Can be used during application startup or runtime.

Why Would We List All Beans?

In a Spring Boot application, many beans are created automatically:

• Controllers • Services • Repositories • Configuration classes • Auto-configured beans

Sometimes developers need to:

• Verify bean creation • Debug dependency injection issues • Understand auto-configuration behavior • Inspect Spring's internal components

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.

Code Example:

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

Output Example:

employeeController

employeeService

employeeRepository

dataSource

entityManagerFactory

dispatcherServlet

These are some of the beans managed by Spring.

Displaying Beans at Startup

A common approach is to use CommandLineRunner.

Code Example:

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

Code Example:

int count =
        applicationContext
                .getBeanDefinitionCount();

System.out.println(
        "Total Beans: " + count);

Output:

Total Beans: 250

The exact number depends on the application's dependencies and configurations.

Example: Suppose an application contains:

• EmployeeController • EmployeeService • EmployeeRepository

Spring Boot also creates additional infrastructure beans such as:

• DataSource • DispatcherServlet • TransactionManager

Using ApplicationContext helps verify that all required beans are successfully loaded.

Alternative Using Actuator

If Spring Boot Actuator is enabled:

Endpoint:

/actuator/beans

This endpoint displays:

• Bean names • Bean types • Dependencies

without writing any custom code.

Benefits

• Easier debugging • Better understanding of Spring internals • Verification of bean registration • Troubleshooting dependency injection issues • Useful during development and learning

Real-World Example

Suppose a Service bean is not being injected correctly.

By listing all beans, developers can quickly verify:

• Whether the bean exists. • Whether component scanning detected it. • Whether Spring created the bean successfully.

This helps identify configuration problems much faster.

Interview Tip: 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."