Skip to content

DurgeshOnStack/FactoryBean_01Vehicle_Engine_Factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

🚗 Vehicle Engine Factory (Spring Core Project)

A simple Spring Core (Java-based configuration) project demonstrating the use of FactoryBean to dynamically create different types of engine objects (Diesel, Electric, Hybrid) based on external configuration.


📌 Overview

This project showcases how to:

  • Use Spring's FactoryBean interface to control object creation.
  • Externalize configuration using application.properties.
  • Achieve loose coupling via interfaces and dependency injection.
  • Dynamically switch implementations without changing code.

⚙️ Tech Stack

  • Java
  • Spring Core (Annotation-based configuration)
  • Maven (optional, if you add build support)

📂 Project Structure

com.nit
│
├── config
│   └── AppConfig.java
│
├── factory
│   └── VehicleEngineFactoryBean.java
│
├── sbeans
│   ├── Engine.java
│   ├── DieselEngine.java
│   ├── ElectricEngine.java
│   └── HybridEngine.java
│
└── main
    └── TestApp.java

🧠 Core Concept

Instead of directly creating objects using new, we let Spring decide which engine to create at runtime using a custom FactoryBean.

🔑 Key Logic

  • The engine type is read from properties:

    engine.type=diesel
  • Based on this value, the factory returns the correct implementation:

if(engineType.equalsIgnoreCase("electric")) {
    return new ElectricEngine();
}
else if(engineType.equalsIgnoreCase("diesel")) {
    return new DieselEngine();
}
else if(engineType.equalsIgnoreCase("hybrid")) {
    return new HybridEngine();
}

📁 Important Files

🔹 Configuration Class

  • Loads properties and configures the FactoryBean

🔹 FactoryBean Implementation

  • Central place where object creation logic exists

🔹 Engine Interface

  • Common contract for all engine types

🔹 Engine Implementations

  • Diesel Engine →
  • Electric Engine →
  • Hybrid Engine →

🔹 Main Class

  • Bootstraps Spring container and runs the app

▶️ How to Run

  1. Clone the repository

  2. Open in your IDE (Eclipse / IntelliJ)

  3. Set engine type in application.properties:

    engine.type=electric
  4. Run:

    TestApp.java
    

🔄 Output Example

If engine.type=diesel

DieselEngine has started.
DieselEngine has stoped.

If engine.type=electric

ElectricEngine has started.
ElectricEngine has stoped.

🎯 Why This Matters

This pattern is super useful when:

  • You want runtime flexibility
  • You want to follow SOLID principles
  • You need clean separation of object creation logic

🚀 Future Improvements

  • Add Spring Boot support
  • Use profiles instead of properties
  • Add logging framework (SLF4J / Logback)
  • Introduce dependency injection into engines

📜 License

This project is for educational purposes.


💡 Final Thought

Small project, but the concept? 🔥 Understanding FactoryBean gives you real control over how Spring creates and manages objects — that’s where things start getting powerful.


About

A Spring Core project that uses a custom FactoryBean to dynamically create different engine implementations (Diesel, Electric, Hybrid) based on external configuration. It demonstrates loose coupling and runtime flexibility by selecting the desired engine via properties without changing the application code.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages