Spring Boot - listing beans

In this tutorial, we list all beans stored in the Spring container, including built-in and custom beans.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring which helps create stand-alone, production-grade Spring based applications with minimal effort.

The core Spring containter creates and manages beans. The beans are available throught ApplicationContext. In the following application, we list all stored beans. The application is command line Spring Boot application.

$ tree
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           ├── bean
    │   │           │   └── MyBean.java
    │   │           └── MyRunner.java
    │   └── resources
    └── test
        └── java

This is the project structure of the Spring Boot application.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 



This is the Maven build file. The spring-boot-starter is the core starter that includes auto-configuration support, logging, and YAML. The application is packaged into a JAR file.

package com.zetcode.bean;

import org.springframework.stereotype.Component;

public class MyBean {
    private final String message = "This is MyBean";
    public String getMessage() {
        return message;

The MyBean is a custom bean that is created and managed by Spring. Classes decorated by @Component annotation are auto-detected by Spring and stored in Spring container.

package com.zetcode;

import com.zetcode.bean.MyBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

public class MyRunner implements CommandLineRunner {

    private ApplicationContext appContext;
    private MyBean myBean;

    public void run(String... args) throws Exception {
        System.out.println("List of beans:");
        String[] beans = appContext.getBeanDefinitionNames();
        for (String bean : beans) {

The CommandLineRunner interface indicates that a bean should run when it is contained within a SpringApplication. It can be used to create Spring Boot command line applications.

public class MyRunner implements CommandLineRunner {

MyRunner is a Spring bean as well and is listed among the beans.

private ApplicationContext appContext;

The ApplicationContext is injected into the field with the @Autowired annotation.

private MyBean myBean;

Likewise, we inject our custom bean.


Here we print the message stored in our custom bean.

String[] beans = appContext.getBeanDefinitionNames();

From the application context, we get an array of bean names with the getBeanDefinitionNames().

for (String bean : beans) {

The bean names are printed to the console.

package com.zetcode;

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

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

In the Application, set up the Spring Boot application. The set up the Spring Boot application. The @SpringBootApplication enables auto-configuration and component scanning. Spring will automatically scan for beans and will pick up both MyBean and MyRunner.

$ mvn spring-boot:run -q
This is MyBean
List of beans:

We run the application. The -q Maven option turns off Maven messages.

In this tutorial, we have listed all beans stored in Spring container. You might also be interested in the related tutorials: Spring Boot Bean, Spring Boot @Qualifier annotation, Serving static content in Spring Boot, Spring Boot DataSourceBuilder tutorial, Spring Boot iText tutorial, Java tutorial.