Skip to content

DurgeshOnStack/Scope_Project02_CabBookingPrototypeApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

🚖 Cab Booking System (Spring Core - Annotation Based)

A simple Spring Core (Annotation-based) Java application that demonstrates Dependency Injection (DI), Bean Scopes, and Component Scanning using a cab booking scenario.

This project models a basic ride-booking system where a Driver is assigned to a ride and executes cab-driving operations.


📌 Features

  • Annotation-based Spring configuration (no XML)
  • Demonstrates @Component, @Autowired, and @Scope
  • Shows Singleton Bean Scope behavior
  • Clean separation of configuration and business logic
  • Simple and beginner-friendly structure

🛠️ Tech Stack

  • Java
  • Spring Framework (Core)
  • Maven (optional, if you add dependencies)

📂 Project Structure

com.nit
│
├── config
│   └── AppConfig.java
│
├── main
│   └── TestApp.java
│
└── sbeans
    ├── Driver.java
    └── RideBooking.java

⚙️ Configuration

AppConfig.java

  • Enables component scanning for Spring Beans
@Configuration
@ComponentScan(basePackages="com.nit.sbeans")
public class AppConfig {
}

🚗 Core Components

Driver.java

  • Represents a cab driver
  • Configured as a Singleton Bean
@Component
@Scope("singleton")
public class Driver {
    private String rideId;
    private String driverName;
    private String cabNumber;

    public void setDriver(String rideId, String driverName, String cabNumber) {
        this.rideId = rideId;
        this.driverName = driverName;
        this.cabNumber = cabNumber;
    }

    public void driveCab() {
        System.out.println("Your Cab Is On The Way....");
        System.out.println("Your Ride Id: "+rideId);
        System.out.println("Your Driver Name: "+driverName);
        System.out.println("Your Cab Number: "+cabNumber);
    }
}

RideBooking.java

  • Handles ride booking logic
  • Uses Dependency Injection (@Autowired) to inject Driver
@Component
public class RideBooking {
    private String rideId;
    private String pickupLocation;

    @Autowired
    private Driver driver;

    public void setRideBooking(String rideId, String pickupLocation) {
        this.rideId = rideId;
        this.pickupLocation = pickupLocation;
    }

    public void bookRide() {
        System.out.println("===Your Ride Is Booked===");
        System.out.println("Your Ride ID: "+rideId);
        System.out.println("Your pickup location: "+pickupLocation);
        driver.driveCab();
    }
}

▶️ Main Class

TestApp.java

  • Bootstraps Spring Container
  • Retrieves multiple Driver beans
  • Demonstrates Singleton Scope (same object instance)
public class TestApp {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
            new AnnotationConfigApplicationContext(AppConfig.class);

        Driver driver1 = ctx.getBean(Driver.class);
        Driver driver2 = ctx.getBean(Driver.class);
        Driver driver3 = ctx.getBean(Driver.class);

        driver1.setDriver("D101","Raj","UP202");
        driver2.setDriver("D202","Rahul","UP404");
        driver3.setDriver("D303","Ramesh","UP808");

        driver1.driveCab();
        driver2.driveCab();
        driver3.driveCab();

        System.out.println("Driver 1 hash code: "+driver1.hashCode());
        System.out.println("Driver 2 hash code: "+driver2.hashCode());
        System.out.println("Driver 3 hash code: "+driver3.hashCode());
    }
}

🧠 Key Concepts Demonstrated

1. Dependency Injection

Spring automatically injects the Driver into RideBooking using @Autowired.

2. Component Scanning

Spring scans com.nit.sbeans package to detect beans using:

@ComponentScan(basePackages="com.nit.sbeans")

3. Bean Scope (Singleton)

Even though multiple beans are requested:

ctx.getBean(Driver.class);

➡️ All references point to the same object instance


🧪 Sample Output

Your Cab Is On The Way....
Your Ride Id: D303
Your Driver Name: Ramesh
Your Cab Number: UP808

Driver 1 hash code: 12345678
Driver 2 hash code: 12345678
Driver 3 hash code: 12345678

👉 Notice all hash codes are identical → confirms Singleton scope.


🚀 How to Run

  1. Clone the repository
  2. Open in any Java IDE (IntelliJ / Eclipse)
  3. Add Spring dependencies if not already included
  4. Run TestApp.java

📌 Future Improvements

  • Add Prototype scope example
  • Introduce interfaces for loose coupling
  • Add Spring Boot version
  • Integrate database for real ride storage

🤝 Contributing

Pull requests are welcome. Feel free to improve or extend the project.


📄 License

This project is open-source and free to use for learning purposes.


💡 Author

Developed as a learning project for understanding Spring Core fundamentals.


About

A simple Spring Core application demonstrating Dependency Injection, component scanning, and singleton bean scope using a cab booking scenario. It shows how multiple bean requests return the same instance while managing driver and ride details.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages