This library offer several types of injection:
- Constructors injection
- Setters injection
- Properties injection
- Method injection
The constructor injection try to create a class instance by looping over every class constructor until it found one that can be used .
The injection criteria are:
- Parameters are not Java primitive
- Parameters packages are in injector package list
- All parameters constructor do the same
The setter injection is automatically run after the constructor injection if the injector have the option activated.
(Can be also be call on an existing instance)
For a setter method to be injected, it must validate the following conditions:
- The method name MUST start with
set
- The method MUST have exactly one parameter
- The method MUST have the annotation
@javax.inject.Inject
- The method parameter must be an injectable class
The property injection is automatically run after the constructor injection if the injector have the option activated.
(Can be also be call on an existing instance)
For a property to be injected, it must validate the following conditions:
- The property MUST be accessible
- The property MUST have the annotation
@javax.inject.Inject
- The property must be an injectable class
A method can have its parameters injected.
There are two way to inject parameters in a method.
First one is with a java.lang.reflect.Method
object, in this case there are no control, if a parameter can't be injected null
will be used.
The second way is to use the method name, with this way, all method of the object with this name will be try, and the method must have the annotation @javax.inject.Inject
and every parameters must be injectable.
There are two injection types:
- Singleton
- Every times a new instance
Clone the project:
git clone https://github.com/MacFJA/Injector.git
Install the project into your local Maven repository:
cd Injector/
mvn clean
mvn install
Remove the source:
cd ..
rm -r Injector/
Add the depency in your Maven project:
<project>
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>io.github.macfja</groupId>
<artifactId>injector</artifactId>
<version>1.1.0</version>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
</project>
io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage");
injector.addMapping(new mypackage.MyClass());
// ... later
injector.get(mypackage.MyClass.class); // return the instance created in addMapping method
// ... later
injector.get(mypackage.MyClass.class); // still the same instance
or
io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage");
injector.addMapping(mypackage.MyClass.class, new io.github.macfja.injector.InjectionUnit(new mypackage.MyClass()));
// ... later
injector.get(mypackage.MyClass.class); // return the instance created in addMapping method
// ... later
injector.get(mypackage.MyClass.class); // still the same instance
or
io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage");
injector.addMapping(
mypackage.MyClass.class,
new io.github.macfja.injector.InjectionUnit(
mypackage.MyClass.class,
io.github.macfja.injector.InjectionUnit.Instantiation.Singleton
)
);
// ... later
injector.get(mypackage.MyClass.class); // create a new instance (first call)
// ... later
injector.get(mypackage.MyClass.class); // still the same instance
or
io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage");
injector.addMapping(
mypackage.MyClass.class,
io.github.macfja.injector.InjectionUnit.Instantiation.Singleton
);
// ... later
injector.get(mypackage.MyClass.class); // create a new instance (first call)
// ... later
injector.get(mypackage.MyClass.class); // still the same instance
io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage");
injector.addMapping(mypackage.MyInterface.class, new io.github.macfja.injector.InjectionUnit(/* ... */));
// ... later
injector.get(mypackage.MyInterface.class); // return an instance according to the InjectionUnit