Note: This IoC container implementation is for educational purposes and not designed for practical usage in production applications. It does not handle circular dependencies.
This is a basic implementation of an IoC (Inversion of Control) container in Java. The primary purpose of this project is to help you understand the fundamental concepts of IoC containers, dependency injection, and how they work in Java.
-
Component Scanning: The container can scan and discover classes annotated with
@Component
in your project. -
Dependency Injection: It supports constructor-based dependency injection for the discovered components.
-
Annotations: You can use
@Autowired
for field injection and@Qualifier
to specify which implementation to use when multiple implementations are available. -
Singleton Scope: Components are created in the singleton scope by default. This means that the container maintains a single instance of each component.
-
Default Implementation: You can use the
@Default
annotation to specify the default implementation for an interface if there are multiple implementations. To specify the interfaces that a class should be the default implementation for, use the@Default
annotation with an array of interface classes. -
Prototype Scope: You can mark components with the
@Prototype
annotation to create a new instance each time it is requested. -
- Qualifier and Name-Based Injection: Fields are injected by name. If a field is not annotated with
@Qualifier
, the container will prioritize matching by field name. If no qualifier and no matching field name are found, the default implementation is used.
- Qualifier and Name-Based Injection: Fields are injected by name. If a field is not annotated with
-
This is a basic and minimalistic IoC container and is not intended for production use. It lacks many features and optimizations found in more mature IoC containers like Spring.
-
Error handling and advanced features like aspect-oriented programming, custom scopes, and circular dependency handling are not implemented.
i encourage you to experiment and learn with this IoC container. Explore the source code, make modifications, and leave a star if u can.
-
Create an instance of the
Container
class and call thegetService
method to retrieve your components. -
The container handles dependency resolution and creates instances of your components as needed.
-
Add the
@Component
annotation to your classes to mark them as components to be managed by the IoC container. -
Use
@Default
to specify default implementations for interfaces.
@Component
@Default({MyInterface1.class, MyInterface2.class})
public class MyComponent implements MyInterface1, MyInterface2 {
// ...
}