Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/macfja/injector
Classes injector. Injection can be done in constructors, setters, properties and methods
https://github.com/macfja/injector
constructor-injection injector property-injection setter-injection singleton
Last synced: 1 day ago
JSON representation
Classes injector. Injection can be done in constructors, setters, properties and methods
- Host: GitHub
- URL: https://github.com/macfja/injector
- Owner: MacFJA
- License: mit
- Created: 2017-05-17T20:48:09.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-23T16:17:11.000Z (about 6 years ago)
- Last Synced: 2024-11-11T02:09:00.712Z (about 1 month ago)
- Topics: constructor-injection, injector, property-injection, setter-injection, singleton
- Language: Java
- Size: 12.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Injector
- [Injection possibility](#injection)
- [Constructor Injection](#injection-constructor)
- [Setters Injection](#injection-setters)
- [Properties Injection](#injection-properties)
- [Method Injection](#injection-method)
- [Injection types](#types)
- [Installation](#installation)
- [Examples](#examples)
- [Declaring a mapping](#examples-mapping)
- [For a singleton](#examples-mapping-singleton)
- [For an interface/abstract class to concrete implementation](#examples-mapping-interface)This library offer several types of injection:
- Constructors injection
- Setters injection
- Properties injection
- Method injectionThe 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 sameThe 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 classThe 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 classA 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 instanceClone 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:
```xml
io.github.macfja
injector
1.1.0
```
```java
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
```java
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
```java
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
```java
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
```#### For an interface/abstract class to concrete implementation
```java
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
```