Get All Spring Managed Beans

Spring Boot loads number of bean as part of the autoconfiguration process. In this article we lean how to get all Spring managed beans using Spring Boot. We will look in the different options for loading these Spring managed beans.

 

Introduction

A bean is the core of Spring IoC container. Spring IoC container manages the life cycle of the beans.In this article, we will all the Spring managed beans using following two approaches

  1. Using Spring’s ApplicationContext and Command Line Runner.
  2. Spring Boot Actuator

 

1. Using ApplicationContext

The first option is use ApplicationContext to get all Spring manged beans. Here are the details:

  1. Get all Spring managed beans using the ApplicationContext.getBeanDefinitionNames().
  2. Iterate through the list to print or get the beans.

We will use command line runner for this:

@SpringBootApplication
public class SpringManagedBeansApplication {

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext applicationContext) {
        return args -> {
            String[] beanNames = applicationContext.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String bean : beanNames) {
                System.out.println(bean);
            }
        };
    }
}

On Running the above example, you may have a similar output in the console:

Get All Spring Managed Beans

If you like to know about the bean class name, use applicationContext.getBean(bean).getClass(); to get the bean class name. Our changed example will look like:

@Bean
    public CommandLineRunner commandLineRunner(ApplicationContext applicationContext) {
        return args -> {
            String[] beanNames = applicationContext.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String bean : beanNames) {
                System.out.println(bean);
                System.out.println(applicationContext.getBean(bean).getClass());
            }
        };
}

 

2. Spring Boot Actuator

Spring Boot Actuator provides production-ready features for Spring Boot application. The actuator provides different end points to monitor your Spring Boot application.One of the end point ‘/bean‘ displays a complete list of all the Spring managed beans in our application. Once the actuator is active, hit the http://<address>:<management-port>/actuator/beans.This will return the JSON response with all Spring managed bean under the IoC container.

This is how the response look like

{
    "contexts": {
        "application": {
            "beans": {
                "endpointCachingOperationInvokerAdvisor": {
                    "aliases": [],
                    "scope": "singleton",
                    "type": "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
                    "resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
                    "dependencies": [
                        "environment"
                    ]
                },
                "defaultServletHandlerMapping": {
                    "aliases": [],
                    "scope": "singleton",
                    "type": "org.springframework.web.servlet.HandlerMapping",
                    "resource": "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]",
                    "dependencies": []
                },
                "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration": {
                    "aliases": [],
                    "scope": "singleton",
                    "type": "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration$$EnhancerBySpringCGLIB$$1dbc9917",
                    "resource": null,
                    "dependencies": [
                        "spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties"
                    ]
                },
                "metricsRestTemplateCustomizer": {
                    "aliases": [],
                    "scope": "singleton",
                    "type": "org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer",
                    "resource": "class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfiguration.class]",
                    "dependencies": [
                        "simpleMeterRegistry",
                        "restTemplateTagConfigurer"
                    ]
                }
            }
        }
    }
}

This is the truncated version of actual response.

[pullquote align=”normal”]Since Endpoints may contain sensitive information, you may need to enable these end point for your application. [/pullquote]

 

Summary

In this post, we learned how to get all Spring managed beans using Spring Boot. We saw how to use the command line runner and the Spring Boot Actuator to accomplish this task. You can download the source code for this post from GitHub.