Spring Boot listing beans
last modified July 6, 2020
Spring Boot listing beans shows how to 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.
build.gradle gradlew gradlew.bat settings.gradle gradle src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ │ MyRunner.java │ │ └───bean │ │ MyBean.java │ └───resources └───test ├───java └───resources
This is the project structure of the Spring Boot application.
plugins { id 'java' id 'org.springframework.boot' version '2.2.2.RELEASE' id 'io.spring.dependency-management' version '1.0.8.RELEASE' } group 'com.zetcode' version '1.0-SNAPSHOT' sourceCompatibility = 11 repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter' }
This is the Gradle build file. The spring-boot-starter
is the core
starter that includes auto-configuration support, logging, and YAML.
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.
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.
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
.
$ gradlew -q bootRun ... 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 Gradle messages.
In this tutorial, we have listed all beans stored in Spring container.