Open In App

How to Get All Spring-Managed Beans?

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Spring Application, beans are managed by the objects, that are created, configured, and managed by the Spring IOC(Inversion of Control) container. These beans are typically annotated with stereotypes like the @Component, @Service, @Repository, or the @Controller. Sometimes we might need to retrieve the list of all beans managed by the Spring container for the debugging purposes or the dynamic analysis.

Key Terminologies:

  • Spring Bean: An object is managed by the Spring container of the Spring application.
  • Spring IOC Container: The container is responsible for managing the lifecycle of the Spring beans and injecting dependencies of the application.
  • ApplicationContext: The context interface for accessing the beans and their application of application.
  • Component Scan: The process by which the Spring automatically discovers and registers the beans within the application context.

Prerequisites:

  • Basic knowledge of the Spring Boot
  • Good knowledge of the Spring beans annotated with stereotypes
  • Good understanding of Java and Spring framework

Spring can provide several ways to retrieve all the managed beans. One of the common approaches is to access the ApplicationContext which is the central interface for providing the configuration information to the Spring application. The ApplicationContext interface provides the method getBeanDefinitionNames() to retrieve the names of all the beans managed by the container.

Step-by-Step Project Implementation to Get All Spring-Managed Beans

Now, we will develop a simple Spring web application that can Get all the Spring Managed beans of the Spring application.

Step 1: Create the Spring project using spring initializr. On creating the project, add the below dependencies into the project.

Dependencies:

  • Spring Web
  • Spring Dev Tools
  • Lombok

Once, the project created, then the file structure will look like the below.

Folder Structure

Step 2: Open the application.properties file and install the below code in the project for server port and mongodb database configuration.

server.port= 8080


Step 3: Create a new package named as bean. In that package, create a new Java class named as ExampleBean1.

Go to src > org.example.springgetallbeans > bean > ExampleBean1 and put the below code.

Java
package org.example.springgetallbeans.beans;

import org.springframework.stereotype.Component;

@Component("bean1")
public class ExampleBean1 {
    @Override
    public String toString() {
        return "This is ExampleBean1";
    }
}


Step 4: In bean package, create the new Java class named as ExampleBean2.

Go to src > org.example.springgetallbeans > bean > ExampleBean2 and put the below code.

Java
package org.example.springgetallbeans.beans;

import org.springframework.stereotype.Component;

@Component("bean2")
public class ExampleBean2 {
    @Override
    public String toString() {
        return "This is ExampleBean2";
    }
}


Step 5: In bean package, create the new Java class and it named as ExampleBean3.

Go to src > org.example.springgetallbeans > bean > ExampleBean3 and put the below code.

Java
package org.example.springgetallbeans.beans;

import org.springframework.stereotype.Component;

@Component("bean3")
public class ExampleBean3 {
    @Override
    public String toString() {
        return "This is ExampleBean3";
    }
}


Step 6: Create a new package and it named as the controller. In that package, create one new Java class named as BeanController.

Go to src > org.example.springgetallbeans > controller > BeanController and put the below code.

Java
package org.example.springgetallbeans.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BeanController {

    @Autowired
    private ApplicationContext applicationContext;

    // Endpoint to retrieve all bean names managed by the Spring container
    @GetMapping("/beans")
    public String getAllBeans() {
        StringBuilder result = new StringBuilder();
        String[] allBeans = applicationContext.getBeanDefinitionNames();
        for (String beanName : allBeans) {
            result.append(beanName).append("\n");
        }
        return result.toString();
    }

    // Endpoint to retrieve the string representation of bean1
    @GetMapping("/bean1")
    public String getBean1() {
        return applicationContext.getBean("bean1").toString();
    }

    // Endpoint to retrieve the string representation of bean2
    @GetMapping("/bean2")
    public String getBean2() {
        return applicationContext.getBean("bean2").toString();
    }

    // Endpoint to retrieve the string representation of bean3
    @GetMapping("/bean3")
    public String getBean3() {
        return applicationContext.getBean("bean3").toString();
    }
}
  • This BeanController class defines REST endpoints to interact with Spring-managed beans.
  • The /beans endpoint retrieves all bean names registered in the application context.
  • /bean1, /bean2, and /bean3 endpoints fetch the string representations of specific beans (ExampleBean1, ExampleBean2, and ExampleBean3 respectively) using their bean names.
  • These endpoints utilize the ApplicationContext to access the beans and return their string representations.


Step 7: Open the main class and write the below code.

Java
package org.example.springgetallbeans;

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

@SpringBootApplication // Indicates that this is a Spring Boot application and enables auto-configuration
public class SpringGetAllBeansApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringGetAllBeansApplication.class, args); // Launches the Spring Boot application
    }

}


Step 8: Once the spring project completed, run it as spring application. Once it runs successful, then it will start at port 8080.

Application Runs

Endpoints Outputs:

Get the Bean1:

GET http://localhost:8080/bean1

Output in Postman:

Get the Bean1

Get the Bean2:

GET http://localhost:8080/bean2

Output in Postman:

Get the Bean2

Get the Bean3:

GET http://localhost:8080/bean3

Output in Postman:

Get the Bean3

Get All the Beans:

GET http://localhost:8080/beans
Get All the Beans

If we follow the above step, then we can successfully retrieve all the Spring managed Beans of the Spring application.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads