Skip to content

DurgeshOnStack/Scope_Project01_FoodDeliveryScopeApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

🚀 Spring Core – Prototype Scope & Dependency Injection Demo

This project demonstrates core Spring Framework (Spring Core / IoC) concepts such as Annotation-based configuration, Dependency Injection, and Bean Scopes (singleton vs prototype).


📌 Project Overview

This is a simple Java application that simulates an order delivery system where:

  • A Customer places an order
  • A DeliveryService processes and delivers the order

The main goal is to understand how Spring manages object creation and dependency injection using annotations.


🧠 Key Concepts Covered

  • ✅ Annotation-based configuration (@Configuration, @ComponentScan)

  • ✅ Dependency Injection using @Autowired

  • ✅ Bean Scopes:

    • Singleton (default scope)
    • Prototype
  • ✅ Spring IoC Container usage


📂 Project Structure

com.nit
│
├── config
│   └── AppConfig.java
│
├── main
│   └── TestApp.java
│
└── sbeans
    ├── Customer.java
    └── DeliveryService.java

⚙️ Configuration

Spring configuration is done using Java annotations:

@Configuration
@ComponentScan(basePackages = "com.nit.sbeans")
public class AppConfig {
}

This tells Spring to scan the specified package for components.


🔗 Dependency Injection

The Customer class depends on DeliveryService, which is injected automatically:

@Autowired
private DeliveryService deliveryService;

🔁 Bean Scope Behavior

🔹 Customer (Prototype Scope)

@Scope("prototype")
  • A new object is created every time the bean is requested from the container.

🔹 DeliveryService (Singleton Scope - Default)

  • Only one shared object exists throughout the application.

▶️ Application Flow

  1. Spring container is initialized using AnnotationConfigApplicationContext
  2. Two DeliveryService beans are requested → SAME instance
  3. Two Customer beans are requested → DIFFERENT instances
  4. Each customer places an order
  5. Output confirms scope behavior using hashcodes

▶️ How to Run

✅ Prerequisites

  • Java 8 or higher
  • IDE (Eclipse / IntelliJ / VS Code)

▶️ Steps

  1. Clone the repository
  2. Open in your IDE
  3. Run TestApp.java

OR via terminal:

javac *.java
java com.nit.main.TestApp

📊 Sample Output

====Sevice Details===
Order is placing...
Service Name: Swiggy Delivery
Total no of orders : 2
Order is Delivered
-----Customer Detail----
Customer ID: CUS101
Customer Name: Raj

====Sevice Details===
Order is placing...
Service Name: Swiggy Delivery
Total no of orders : 2
Order is Delivered
-----Customer Detail----
Customer ID: CUS103
Customer Name: Rahul

Hash code of customer1: 12345678
Hash code of customer2: 87654321

🔍 Key Observations

  • Customer beans have different hashcodes → Prototype scope works
  • DeliveryService behaves like a shared object → Singleton scope
  • ✔ Even multiple calls to getBean() return the same DeliveryService instance

💡 Learning Outcome

By completing this project, you will understand:

  • How Spring IoC container manages beans
  • Difference between Singleton and Prototype scope
  • Real-world usage of Dependency Injection
  • Annotation-based configuration in Spring

🛠️ Tech Stack

  • Java
  • Spring Core (IoC Container)
  • Annotation-based configuration

📬 Author

This project is created for learning and practicing Spring Core fundamentals.


⭐ Feel free to fork, modify, and experiment with this project!

About

A simple Spring Core project demonstrating Dependency Injection and bean scopes using annotation-based configuration. It shows how prototype-scoped Customer objects interact with a singleton DeliveryService within the Spring IoC container.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages