This is an object injector for Java programs that is used to inject objects into static fields of classes, instead of passing them as arguments to constructors. This is useful when you have a lot of classes that need to access the same object, but you don't want to pass it as an argument to every constructor.
If you use Maven you should use the following code :
<repositories>
...
<repository>
<id>ekalia</id>
<url>https://nexus.ekalia.fr/repository/maven-public/</url>
</repository>
...
</repositories>
<dependencies>
...
<dependency>
<groupId>fr.ekalia.injector</groupId>
<artifactId>ekalia-injector</artifactId>
<version>latest</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
If you use Gradle you should use the following code :
Groovy:
repositories {
maven {
url = uri("https://nexus.ekalia.fr/repository/maven-public/")
}
}
dependencies {
compileOnly("fr.ekalia.injector:ekalia-injector:latest")
}
Kts:
repositories {
maven("https://nexus.ekalia.fr/repository/maven-public/")
}
dependencies {
compileOnly("fr.ekalia.injector:ekalia-injector:latest")
}
In order to use the injector, you need to create an instance of the Injector
class.
Injector injector = new Injector();
You can add class loaders to the injector to load classes from different sources:
injector.addClassLoader(myClassLoader);
Then you can start the injector:
ClassLoader currentClassLoader = this.getClass().getClassLoader();
injector.startInjection(currentClassLoader, "my.really.cool.package", "my.other.cool.package");
When you start the injector, it will scan all the classes in the class loaders and inject the provided classes into the fields of the classes that have the @Inject
annotation.
You have 2 options to provide a class to the injector:
If you want the class to be automatically provided by the injector, you can use the @Provides
annotation like this:
@Provides
public class MyProvidedClass {
// ...
}
When you start the injector, it will automatically create an instance of this class using the default constructor.
If you want to provide the class manually, you can use the registerInjection
method like this:
injector.registerInjection(myProvidedClassInstance);
If you want to set a priority (see below for explanations about priority) for the provided class, you can use the registerInjection
method like this:
injector.registerInjection(myProvidedClassInstance, InjectPriority.HIGH);
To mark a field for injection, you need to use the @Inject
annotation like this:
public class MyClass {
@Inject
private static MyProvidedClass myProvidedClass;
// ...
}
When you start the injector, it will automatically inject the provided class into the field.
The injector uses priorities to determine which provided class to inject into a field. The default priority is InjectPriority.NORMAL
.
The priorities are:
InjectPriority.LOWEST
InjectPriority.LOW
InjectPriority.NORMAL
InjectPriority.HIGH
InjectPriority.HIGHEST
The order of priorities is from lowest to highest: if a field has multiple provided classes with different priorities, the one with the highest priority will be injected.
You can set the priority of a provided class when you register it with the injector or in the @Provides
annotation:
@Provides(priority = InjectPriority.HIGH)
public class MyProvidedClass {
// ...
}
Issues and pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details.