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

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages