Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Java-Based Configuration

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.


📖 Project Overview

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).


🎯 Learning Objectives

  • 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

🏗️ Project Structure

src
└── main
    ├── java
    │
    │   └── org.officeflow
    │
    │       ├── app
    │       │      Main.java
    │       │
    │       ├── employee
    │       │      Employee.java
    │       │
    │       ├── department
    │       │      Department.java
    │       │
    │       ├── laptop
    │       │      Laptop.java
    │       │
    │       └── config
    │              AppConfig.java
    │
    └── resources

📚 Object Relationship

Employee
    │
    ▼
Department
    │
    ▼
Laptop

Spring creates the complete object graph using Java configuration and constructor dependency injection.


⚙️ Technologies Used

  • Java 21
  • Spring Framework (Spring Core)
  • Maven
  • Java-Based Configuration
  • IntelliJ IDEA

🚀 Features

  • Java-Based Bean Configuration
  • Constructor Dependency Injection
  • Spring IoC Container
  • Explicit Bean Registration
  • Loose Coupling
  • XML-Free Configuration

📝 Spring Annotations Used

@Configuration

Marks a class as the source of bean definitions.

@Configuration
public class AppConfig {
}

@Bean

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);
}

▶️ How to Run

  1. Clone the repository.
  2. Open the project in IntelliJ IDEA.
  3. Download Maven dependencies.
  4. Run Main.java.
  5. Spring initializes the IoC Container using AppConfig.
  6. Retrieve the Employee bean.
  7. Display employee details.

📌 Key Concepts Practiced

  • Spring IoC Container
  • Java-Based Configuration
  • Bean Registration
  • Constructor Dependency Injection
  • @Configuration
  • @Bean
  • AnnotationConfigApplicationContext
  • Explicit Bean Creation
  • Loose Coupling

🔄 Learning Progression

Manual Dependency Injection

Objects were created manually using the new keyword.

Laptop laptop = new Laptop();
Department department = new Department(laptop);
Employee employee = new Employee(department);

Spring XML Configuration

Bean creation was moved to XML.

<bean id="employee" class="...Employee"/>

Spring Annotation Configuration

Spring automatically discovered beans using annotations.

@Component
public class Employee {
}

Spring Java Configuration

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.


📈 Learning Outcomes

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

📚 Comparison

Configuration Style Bean Registration
Manual DI new keyword
XML Configuration <bean>
Annotation Configuration @Component + Component Scanning
Java Configuration @Configuration + @Bean

🔜 Next Steps

The next project in this learning journey is:

Spring Bean Scopes

Topics to explore:

  • Singleton Scope
  • Prototype Scope
  • Bean Lifecycle
  • Spring Bean Management

📂 Repository Purpose

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.

About

A Spring Core project demonstrating Java-based bean configuration using @configuration and @bean, eliminating XML while understanding how Spring manages beans through Java code.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages