A Spring Core project demonstrating Java-based bean configuration using @Configuration and @Bean, eliminating XML configuration while understanding how the Spring IoC Container creates and manages beans through Java code.
This project is part of my Spring Framework learning journey.
The objective is to understand how Spring manages beans using Java Configuration instead of XML or component scanning.
The application simulates a simple Office Management System consisting of:
- Employee
- Department
- Laptop
Spring creates and manages all beans using a Java configuration class (AppConfig).
- Understand Java-Based Configuration
- Learn the purpose of
@Configuration - Learn the purpose of
@Bean - Understand Bean Registration
- Learn Constructor Dependency Injection using
@Bean - Understand
AnnotationConfigApplicationContext - Compare XML, Annotation, and Java-Based Configuration
src
└── main
├── java
│
│ └── org.officeflow
│
│ ├── app
│ │ Main.java
│ │
│ ├── employee
│ │ Employee.java
│ │
│ ├── department
│ │ Department.java
│ │
│ ├── laptop
│ │ Laptop.java
│ │
│ └── config
│ AppConfig.java
│
└── resources
Employee
│
▼
Department
│
▼
Laptop
Spring creates the complete object graph using Java configuration and constructor dependency injection.
- Java 21
- Spring Framework (Spring Core)
- Maven
- Java-Based Configuration
- IntelliJ IDEA
- Java-Based Bean Configuration
- Constructor Dependency Injection
- Spring IoC Container
- Explicit Bean Registration
- Loose Coupling
- XML-Free Configuration
Marks a class as the source of bean definitions.
@Configuration
public class AppConfig {
}Registers an object as a Spring-managed bean.
@Bean
public Laptop laptop() {
return new Laptop();
}Dependencies are injected automatically through method parameters.
@Bean
public Department department(Laptop laptop) {
return new Department(laptop);
}- Clone the repository.
- Open the project in IntelliJ IDEA.
- Download Maven dependencies.
- Run
Main.java. - Spring initializes the IoC Container using
AppConfig. - Retrieve the
Employeebean. - Display employee details.
- Spring IoC Container
- Java-Based Configuration
- Bean Registration
- Constructor Dependency Injection
@Configuration@BeanAnnotationConfigApplicationContext- Explicit Bean Creation
- Loose Coupling
Objects were created manually using the new keyword.
Laptop laptop = new Laptop();
Department department = new Department(laptop);
Employee employee = new Employee(department);Bean creation was moved to XML.
<bean id="employee" class="...Employee"/>Spring automatically discovered beans using annotations.
@Component
public class Employee {
}Bean creation is defined entirely in Java.
@Configuration
public class AppConfig {
@Bean
public Laptop laptop() {
return new Laptop();
}
@Bean
public Department department(Laptop laptop) {
return new Department(laptop);
}
@Bean
public Employee employee(Department department) {
return new Employee(department);
}
}No XML configuration is required.
After completing this project, I gained practical experience with:
- Java-based Spring configuration
- Bean registration using
@Bean - Creating configuration classes with
@Configuration - Constructor dependency injection
- Spring IoC Container
- Eliminating XML configuration
| Configuration Style | Bean Registration |
|---|---|
| Manual DI | new keyword |
| XML Configuration | <bean> |
| Annotation Configuration | @Component + Component Scanning |
| Java Configuration | @Configuration + @Bean |
The next project in this learning journey is:
Spring Bean Scopes
Topics to explore:
- Singleton Scope
- Prototype Scope
- Bean Lifecycle
- Spring Bean Management
This repository is part of my structured journey to learn the Spring Framework from fundamentals before moving to Spring Boot, Spring Data JPA, Spring Security, and Microservices.