Skip to content

Commit 600d58a

Browse files
committed
Initial commit
0 parents  commit 600d58a

File tree

11 files changed

+1512
-0
lines changed

11 files changed

+1512
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Maven template
3+
target/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2017 [MacFJA](https://github.com/MacFJA)
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
```

doc/Example1.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Example 1
2+
3+
This example is a case where method injection is used (differing class loading).
4+
5+
This approach can also be used for lazy loading.
6+
7+
```java
8+
package example;
9+
10+
class PanelA extends javax.swing.JPanel {
11+
// Do your stuff
12+
}
13+
14+
class PanelB extends javax.swing.JPanel {
15+
// Do other stuff
16+
}
17+
18+
class Application extends javax.swing.JFrame {
19+
private static io.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector("example");
20+
21+
public static void main(String[] args) {
22+
injector.addMapping(
23+
Application.class,
24+
new io.github.macfja.injector.InjectionUnit(
25+
Application.class,
26+
io.github.macfja.injector.InjectionUnit.Instantiation.Singleton
27+
)
28+
);
29+
30+
injector.get(Application.class);
31+
32+
}
33+
34+
public Application(PanelA panel) {
35+
setContentPane(panel);
36+
setVisible(true);
37+
38+
new java.util.Timer().schedule(new java.util.TimerTask() {
39+
@Override
40+
public void run() {
41+
injector.injectIntoMethodName(Application.this, "init");
42+
}
43+
}, 1000);
44+
}
45+
46+
public void init(PanelB panelB) {
47+
setContentPane(panelB);
48+
}
49+
}
50+
```

doc/Example2.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Example 2
2+
3+
This example show the interface use case.
4+
5+
```java
6+
package example;
7+
interface ServiceInterface {
8+
void connect();
9+
void disconnect();
10+
void doAction(String param);
11+
}
12+
class Application {
13+
private ServiceInterface service;
14+
public static io.github.macfja.injector.Injector injector;
15+
16+
public static void main(String[] args) {
17+
injector = new io.github.macfja.injector.Injector("example");
18+
injector.addMapping(
19+
ServiceInterface.class,
20+
new io.github.macfja.injector.InjectionUnit(
21+
SocketService.class,
22+
io.github.macfja.injector.InjectionUnit.Instantiation.Singleton
23+
)
24+
);
25+
26+
injector.get(Application.class);
27+
28+
}
29+
30+
public Application(ServiceInterface service) {
31+
this.service = service;
32+
33+
doStuff();
34+
}
35+
36+
37+
public void doStuff() {
38+
service.connect();
39+
service.doAction("first");
40+
service.doAction("second");
41+
service.disconnect();
42+
}
43+
}
44+
45+
class SocketService implements ServiceInterface {
46+
@Override
47+
public void connect() {
48+
// Open socket
49+
}
50+
51+
@Override
52+
public void doAction(String param) {
53+
// Send data in socket
54+
}
55+
56+
@Override
57+
public void disconnect() {
58+
// Close socket
59+
}
60+
}
61+
62+
class FileService implements ServiceInterface {
63+
@Override
64+
public void connect() {
65+
// Open file stream
66+
}
67+
68+
@Override
69+
public void doAction(String param) {
70+
// Write in file
71+
}
72+
73+
@Override
74+
public void disconnect() {
75+
// Close file stream
76+
}
77+
}
78+
```

doc/Mapping.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Mapping
2+
3+
A mapping is a rule that indicate the _Injector_ how to handle a class.
4+
5+
There are several mapping:
6+
7+
- The singleton mapping (for a given class, the same instance is always return)
8+
- The interface to implementation (when the interface is requested a new instance of a defined class is provided)
9+
- The interface to singleton (when the interface is requested a singleton is provided)
10+
- The parent to child (when requested, a new instance of the defined class children is returned)
11+
- The parent to child singleton (when requested, a singleton of the defined class children is returned)
12+
13+
## Interface and parent mapping
14+
15+
The **interface to implementation**, the **interface to singleton**, the **parent to child** and the **parent to child singleton** are base on the same principle.
16+
The method `addMapping` is used in its form:
17+
```
18+
public void addMapping(Class, InjectionUnit)
19+
```
20+
Where the first parameter is the interface/parent and the second contains information about implementation/child/singleton.
21+
22+
```
23+
| First parameter | Second parameter
24+
-----------------------------+-------------------+------------------------------------------------------------------------------
25+
interface to implementation | MyInterface.class | new InjectionUnit(MyImpl.class, InjectionUnit.Instantiation.NewInstance)
26+
interface to singleton | MyInterface.class | new InjectionUnit(MyImpl.class, InjectionUnit.Instantiation.Singleton)
27+
interface to singleton | MyInterface.class | new InjectionUnit(new MyImpl())
28+
parent to child | MyAbstract.class | new InjectionUnit(MyExtender.class, InjectionUnit.Instantiation.NewInstance)
29+
parent to child singleton | MyAbstract.class | new InjectionUnit(MyExtender.class, InjectionUnit.Instantiation.Singleton)
30+
parent to child singleton | MyAbstract.class | new InjectionUnit(new MyExtender())
31+
```
32+
33+
## Singleton mapping
34+
35+
There are 3 way to declare a Singleton:
36+
37+
```
38+
injector.addMapping(new MySingleton());
39+
```
40+
41+
```
42+
injector.addMapping(MySingleton.class, new InjectionUnit(new MySingleton()));
43+
// or
44+
injector.addMapping(MyInterface.class, new InjectionUnit(new MyImplementation()));
45+
```
46+
47+
```
48+
injector.addMapping(MySingleton.class, new InjectionUnit(MySingleton.class, InjectionUnit.Instantiation.Singleton));
49+
// or
50+
injector.addMapping(MyInterface.class, new InjectionUnit(MyImplementation.class, InjectionUnit.Instantiation.Singleton));
51+
```

0 commit comments

Comments
 (0)