Simple Dependency Injector is a Dependency Injector built in Kotlin/Java to be used in small projects.
There are two main annotations:
- Component: Used to annotate the classes that should be injected by the injector. This annotation should be used in implementation classes or in classes that the user wants to be instantiated by the injector (like a Bean)
- Inject: Used to annotate the classes to be injected by the injector.
The way to use the @Component
annotation is the following:
@Component(classes = [MyService::class])
It is necessary to provide a class or classes that the component implements, so the injector could track the different implementations. Also, if it is necessary to just track a class that does not implement an interface, it is possible by setting the own class
@Component(classes = [MyClass::class])
The way to use the @Inject
annotation is the following:
@Inject
or
@Inject("com.example.useraccount.services.impl.AccountServiceImpl")
If there is no package name provided in the annotation, the injector will inject the first implementation that it founds. On the other hand, if there is a canonical name in the annotation, the injector will provide that instance.
- Special attention to
Animals
file as is the example of how to configure SDI in your java project.
- Special attention to
Main
file as is the example of how to configure SDI in your kotlin project.