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.
- 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
- Java
- Spring Framework (Core)
- Maven (optional, if you add dependencies)
com.nit
│
├── config
│ └── AppConfig.java
│
├── main
│ └── TestApp.java
│
└── sbeans
├── Driver.java
└── RideBooking.java
- Enables component scanning for Spring Beans
@Configuration
@ComponentScan(basePackages="com.nit.sbeans")
public class AppConfig {
}- 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);
}
}- 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();
}
}- 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());
}
}Spring automatically injects the Driver into RideBooking using @Autowired.
Spring scans com.nit.sbeans package to detect beans using:
@ComponentScan(basePackages="com.nit.sbeans")Even though multiple beans are requested:
ctx.getBean(Driver.class);➡️ All references point to the same object instance
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.
- Clone the repository
- Open in any Java IDE (IntelliJ / Eclipse)
- Add Spring dependencies if not already included
- Run
TestApp.java
- Add Prototype scope example
- Introduce interfaces for loose coupling
- Add Spring Boot version
- Integrate database for real ride storage
Pull requests are welcome. Feel free to improve or extend the project.
This project is open-source and free to use for learning purposes.
Developed as a learning project for understanding Spring Core fundamentals.