|
| 1 | +# Injector |
| 2 | + |
| 3 | + - [Injection possibility](#injection) |
| 4 | + - [Constructor Injection](#injection-constructor) |
| 5 | + - [Setters Injection](#injection-setters) |
| 6 | + - [Properties Injection](#injection-properties) |
| 7 | + - [Method Injection](#injection-method) |
| 8 | + - [Injection types](#types) |
| 9 | + - [Installation](#installation) |
| 10 | + - [Examples](#examples) |
| 11 | + - [Declaring a mapping](#examples-mapping) |
| 12 | + - [For a singleton](#examples-mapping-singleton) |
| 13 | + - [For an interface/abstract class to concrete implementation](#examples-mapping-interface) |
| 14 | + |
| 15 | +## Injection possibility<a id="injection"></a> |
| 16 | + |
| 17 | +This library offer several types of injection: |
| 18 | + |
| 19 | +- Constructors injection |
| 20 | +- Setters injection |
| 21 | +- Properties injection |
| 22 | +- Method injection |
| 23 | + |
| 24 | +### Constructor Injection<a id="injection-constructor"></a> |
| 25 | + |
| 26 | +The constructor injection try to create a class instance by looping over every class constructor until it found one that can be used . |
| 27 | + |
| 28 | +The injection criteria are: |
| 29 | + |
| 30 | +- Parameters are not Java primitive |
| 31 | +- Parameters packages are in injector package list |
| 32 | +- All parameters constructor do the same |
| 33 | + |
| 34 | +### Setters Injection<a id="injection-setters"></a> |
| 35 | + |
| 36 | +The setter injection is automatically run after the constructor injection if the injector have the option activated. |
| 37 | +(Can be also be call on an existing instance) |
| 38 | +For a setter method to be injected, it must validate the following conditions: |
| 39 | + |
| 40 | +- The method name **MUST** start with `set` |
| 41 | +- The method **MUST** have exactly one parameter |
| 42 | +- The method **MUST** have the annotation `@javax.inject.Inject` |
| 43 | +- The method parameter must be an injectable class |
| 44 | + |
| 45 | +### Properties Injection<a id="injection-properties"></a> |
| 46 | + |
| 47 | +The property injection is automatically run after the constructor injection if the injector have the option activated. |
| 48 | +(Can be also be call on an existing instance) |
| 49 | +For a property to be injected, it must validate the following conditions: |
| 50 | + |
| 51 | +- The property **MUST** be accessible |
| 52 | +- The property **MUST** have the annotation `@javax.inject.Inject` |
| 53 | +- The property must be an injectable class |
| 54 | + |
| 55 | +### Method Injection<a id="injection-method"></a> |
| 56 | + |
| 57 | +A method can have its parameters injected. |
| 58 | + |
| 59 | +There are two way to inject parameters in a method. |
| 60 | +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. |
| 61 | +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. |
| 62 | + |
| 63 | +## Injection types<a id="types"></a> |
| 64 | + |
| 65 | +There are two injection types: |
| 66 | + |
| 67 | +- Singleton |
| 68 | +- Every times a new instance |
| 69 | + |
| 70 | +## Installation<a id="installation"></a> |
| 71 | + |
| 72 | +Clone the project: |
| 73 | +``` |
| 74 | +git clone https://github.com/MacFJA/Injector.git |
| 75 | +``` |
| 76 | +Install the project into your local Maven repository: |
| 77 | +``` |
| 78 | +cd Injector/ |
| 79 | +mvn clean |
| 80 | +mvn install |
| 81 | +``` |
| 82 | +Remove the source: |
| 83 | +``` |
| 84 | +cd .. |
| 85 | +rm -r Injector/ |
| 86 | +``` |
| 87 | +Add the depency in your Maven project: |
| 88 | +```xml |
| 89 | +<project> |
| 90 | + <!-- ... --> |
| 91 | + <dependencies> |
| 92 | + <!-- ... --> |
| 93 | + <dependency> |
| 94 | + <groupId>io.github.macfja</groupId> |
| 95 | + <artifactId>injector</artifactId> |
| 96 | + <version>1.0.0</version> |
| 97 | + </dependency> |
| 98 | + <!-- ... --> |
| 99 | + </dependencies> |
| 100 | + <!-- ... --> |
| 101 | +</project> |
| 102 | +``` |
| 103 | + |
| 104 | +## Examples<a id="examples"></a> |
| 105 | + |
| 106 | +### Declaring a mapping<a id="examples-mapping"></a> |
| 107 | + |
| 108 | +#### For a singleton<a id="examples-mapping-singleton"></a> |
| 109 | + |
| 110 | +```java |
| 111 | +io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage"); |
| 112 | +injector.addMapping(new mypackage.MyClass()); |
| 113 | +// ... later |
| 114 | +injector.get(mypackage.MyClass.class); // return the instance created in addMapping method |
| 115 | +// ... later |
| 116 | +injector.get(mypackage.MyClass.class); // still the same instance |
| 117 | +``` |
| 118 | + |
| 119 | +or |
| 120 | + |
| 121 | +```java |
| 122 | +io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage"); |
| 123 | +injector.addMapping(mypackage.MyClass.class, new io.github.macfja.injector.InjectionUnit(new mypackage.MyClass())); |
| 124 | +// ... later |
| 125 | +injector.get(mypackage.MyClass.class); // return the instance created in addMapping method |
| 126 | +// ... later |
| 127 | +injector.get(mypackage.MyClass.class); // still the same instance |
| 128 | +``` |
| 129 | + |
| 130 | +or |
| 131 | + |
| 132 | +```java |
| 133 | +io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage"); |
| 134 | +injector.addMapping( |
| 135 | + mypackage.MyClass.class, |
| 136 | + new io.github.macfja.injector.InjectionUnit( |
| 137 | + mypackage.MyClass.class, |
| 138 | + io.github.macfja.injector.InjectionUnit.Instantiation.Singleton |
| 139 | + ) |
| 140 | +); |
| 141 | +// ... later |
| 142 | +injector.get(mypackage.MyClass.class); // return the instance created in addMapping method |
| 143 | +// ... later |
| 144 | +injector.get(mypackage.MyClass.class); // still the same instance |
| 145 | +``` |
| 146 | + |
| 147 | +#### For an interface/abstract class to concrete implementation<a id="examples-mapping-interface"></a> |
| 148 | + |
| 149 | +```java |
| 150 | +io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("mypackage"); |
| 151 | +injector.addMapping(mypackage.MyInterface.class, new io.github.macfja.injector.InjectionUnit(/* ... */)); |
| 152 | +// ... later |
| 153 | +injector.get(mypackage.MyInterface.class); // return an instance according to the InjectionUnit |
| 154 | +``` |
0 commit comments