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.

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>
    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootListBeans</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>    
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>        

    </dependencies>    

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

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.

MyBean.java
package com.zetcode.bean;

import org.springframework.stereotype.Component;

@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.

MyRunner.java
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;

@Component
public class MyRunner implements CommandLineRunner {

    @Autowired
    private ApplicationContext appContext;
    
    @Autowired
    private MyBean myBean;

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

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.

@Component
public class MyRunner implements CommandLineRunner {

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

@Autowired
private ApplicationContext appContext;

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

@Autowired
private MyBean myBean;

Likewise, we inject our custom bean.

System.out.println(myBean.getMessage());

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) {
    System.out.println(bean);
}

The bean names are printed to the console.

Application.java
package com.zetcode;

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

@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:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
application
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
myRunner
myBean
...

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.