Skip to content

harman-04/spring-ioc-bean-explorer-retrieving-all-beans

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring IoC Bean Explorer

Project Overview

This project is a technical demonstration of the Spring Framework's Inversion of Control (IoC) Container. It focuses on programmatically interacting with the ApplicationContext to retrieve, audit, and display beans managed by the Spring runtime. Unlike standard applications that use beans silently, this project exposes the "brain" of the application via REST endpoints.


Technical Concepts

ApplicationContext

The ApplicationContext is the advanced interface for the Spring IoC container. It is responsible for instantiating, configuring, and assembling beans. In this project, it is used as a registry to look up objects at runtime.

Component Scanning

Spring Boot's @SpringBootApplication annotation triggers a recursive scan starting from the base package (com.spring). It identifies classes marked with @Component and registers them as beans.

RestController

The BeanController uses the @RestController annotation, which combines @Controller and @ResponseBody. This ensures that the data returned by the methods (bean names or descriptions) is written directly into the HTTP response body as plain text or JSON.


File and Class Specifications

1. BeanController.java

  • Purpose: Acts as the interface between the user and the IoC container.
  • Key Methods:
    • getAllBeans(): Utilizes applicationContext.getBeanDefinitionNames() to retrieve an array of all unique identifiers for every bean currently in the container.
    • getBean1(), getBean2(), getBean3(): Demonstrates explicit bean lookup using the getBean(String name) method.

2. Example Beans (1, 2, and 3)

  • Purpose: Represents custom developer-defined components.
  • Annotation: @Component("beanName"). This explicitly names the bean in the container, overriding the default camelCase naming convention.
  • Method: Overrides toString() to provide a descriptive string representation when the bean is retrieved.

3. SpringGetAllBeansApplication.java

  • Purpose: The entry point of the Spring Boot application.
  • Function: Initializes the Spring environment and starts the embedded server.

Dependency Management (pom.xml)

The project utilizes the Spring Boot Starter Parent for dependency management:

  • spring-boot-starter-web: Provides all necessary libraries for building RESTful APIs, including Tomcat and the Spring MVC framework.
  • spring-boot-devtools: Enables hot-swapping of code for a faster development cycle.
  • Lombok: Used for reducing boilerplate code (though minimal in this specific implementation).
  • Java 25: The project is configured to utilize the latest Long Term Support (LTS) version of the Java Development Kit.

API Documentation

Endpoint HTTP Method Description
/beans GET Returns a plain-text list of every bean name registered in the ApplicationContext.
/bean1 GET Returns the string representation of the component named "bean1".
/bean2 GET Returns the string representation of the component named "bean2".
/bean3 GET Returns the string representation of the component named "bean3".

How to Run

  1. Prerequisites: Ensure you have JDK 25 and Maven installed.
  2. Build: Execute mvn clean install in the root directory.
  3. Execute: Run the SpringGetAllBeansApplication.java class from your IDE or use mvn spring-boot:run.
  4. Access: Open a browser or API client and navigate to http://localhost:8080/beans.

Key Observations

Upon accessing the /beans endpoint, you will observe that the list contains far more than the three custom beans created. This includes:

  • Internal Configurations: Beans responsible for error handling, property resolution, and logging.
  • MVC Infrastructure: The DispatcherServlet, HandlerMapping, and ViewResolver beans that enable web functionality.
  • Auto-Configuration: Beans created by Spring Boot based on the dependencies found in the pom.xml.

About

A Spring Boot 3 application demonstrating the internal workings of the IoC Container by programmatically retrieving and listing all managed Beans from the ApplicationContext.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages