This Java project introduces a small but robust Dependency Injection (DI) mechanism, designed to facilitate object management and dependency resolution in Java applications. This DI framework offers a simple yet powerful approach to manage class dependencies, object creation, and lifecycle, enhancing modularity and testability in your Java projects.
- Lazy Loading: Objects are created on-demand, optimizing resource usage.
- Class Mapping: Easily map interfaces or abstract classes to concrete implementations.
- Factory Support: Integrate complex object creation logic with custom factories.
- Field Injection: Automate the injection of dependencies into your classes.
- Custom Object Creation: Control instance creation with custom logic.
- Java Development Kit (JDK)
- An IDE supporting Java (e.g., IntelliJ IDEA, Eclipse).
- Clone the repository:
git clone [repository URL]
- Import the project into your IDE.
-
Object Retrieval:
MyClass myObject = ObjectManager.get(MyClass.class);
-
Class Mapping:
ObjectManager.set(MyInterface.class, MyImplementation.class);
-
Class Mapping:
ObjectManager.set(MyInterface.class, new MyClass());
-
Object Creation with Arguments:
MyClass myObject = ObjectManager.create(MyClass.class, arg1, arg2);
-
Using Factories:
ObjectManager.setFactory(MyClass.class, new MyFactory());
Here's a simple example to demonstrate the usage of the DI mechanism:
-
Define an interface and its implementation:
public interface Logger { void log(String message); } public class ConsoleLogger implements Logger { @Override public void log(String message) { System.out.println(message); } }
-
Configure the DI system:
ObjectManager.set(Logger.class, ConsoleLogger.class);
-
Use the interface in your application:
public class MyApp { @Inject private Logger logger; public void run() { logger.log("Application is running"); } }
-
Retrieve and use the
MyApp
instance:MyApp app = ObjectManager.get(MyApp.class); app.run();
You can find another working example in src/app/Main.java and in the
model
-package.
This project is licensed under the MIT License.