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.
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.
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.
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.
- Purpose: Acts as the interface between the user and the IoC container.
- Key Methods:
getAllBeans(): UtilizesapplicationContext.getBeanDefinitionNames()to retrieve an array of all unique identifiers for every bean currently in the container.getBean1(),getBean2(),getBean3(): Demonstrates explicit bean lookup using thegetBean(String name)method.
- 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.
- Purpose: The entry point of the Spring Boot application.
- Function: Initializes the Spring environment and starts the embedded server.
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.
| 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". |
- Prerequisites: Ensure you have JDK 25 and Maven installed.
- Build: Execute
mvn clean installin the root directory. - Execute: Run the
SpringGetAllBeansApplication.javaclass from your IDE or usemvn spring-boot:run. - Access: Open a browser or API client and navigate to
http://localhost:8080/beans.
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, andViewResolverbeans that enable web functionality. - Auto-Configuration: Beans created by Spring Boot based on the dependencies found in the
pom.xml.