How do you list all the beans in a 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

Why Would We List All Beans?

In a Spring Boot application, many beans are created automatically

Sometimes developers need to

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

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.

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

Output

Total Beans: 250

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

Example

Suppose an application contains

Spring Boot also creates additional infrastructure beans such as

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

without writing any custom code.

Benefits

Real-World Example

Suppose a Service bean is not being injected correctly.

By listing all beans, developers can quickly verify

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.

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?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions