You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#### 4. Use BeanScope to wire and retrieve the beans and use however you wish.
54
+
#### 4. Use BeanScope to wire and retrieve the beans and use them however you wish.
55
55
```java
56
56
BeanScope beanScope =BeanScope.builder().build()
57
57
Example ex = beanScope.get(Example.class);
58
58
```
59
59
60
-
### Example module use
60
+
### Java Module Usage
61
+
When working with Java modules you need to add a `provides` statement in your `module-info.java` with the generated class.
61
62
```java
62
63
importio.avaje.inject.spi.Module;
63
64
@@ -69,23 +70,108 @@ module org.example {
69
70
}
70
71
```
71
72
72
-
## Similar to Dagger(https://google.github.io/dagger/)
73
+
## Similar to [Dagger](https://google.github.io/dagger/)
73
74
74
75
- Uses Java annotation processing for dependency injection
75
76
- Generates source code
76
77
- Avoids any use of reflection or classpath scanning (so low overhead and fast startup)
77
-
- A `Library only` (a DI library and that's it. ~25k in size)
78
-
79
78
80
79
## Differences to Dagger
81
80
82
-
- Aimed specifically for serverside development (rather than Andriod)
81
+
- Aimed specifically for server-side development (rather than Android)
83
82
- Supports lifecycle methods with `@PostConstruct` and `@PreDestory`
84
83
- Supports `@Factory` and `@Bean`
85
84
- Provides API to obtain all bean instances that implement an interface
86
85
- Provides API to obtain all bean instances that have an annotation
87
-
- Integration with serverside web frameworks Javalin, Helidon
86
+
- Integration with server-side web frameworks Javalin, Helidon
88
87
89
88
## Spring DI
90
89
91
90
For comparison with Spring DI look at https://avaje.io/inject/#spring
91
+
92
+
93
+
## Generated Code
94
+
95
+
### DI classes
96
+
97
+
DI classes will be generated to call the constructors for annotated type/factory methods. Below is the class generated for the `Example` class in the above quickstart.
98
+
99
+
```java
100
+
@Generated("io.avaje.inject.generator")
101
+
publicfinalclassExample$DI {
102
+
103
+
/**
104
+
* Create and register Example.
105
+
*/
106
+
publicstaticvoidbuild(Builderbuilder) {
107
+
if (builder.isAddBeanFor(Example.class)) {
108
+
var bean =newExample(builder.get(DependencyClass.class,"!d1"), builder.get(DependencyClass2.class,"!d2"));
109
+
builder.register(bean);
110
+
// depending on the type of bean, callbacks for field/method injection, and lifecycle support will be generated here as well.
111
+
}
112
+
}
113
+
}
114
+
```
115
+
116
+
### Generated Wiring Class
117
+
The inject annotation processor determines the dependency wiring order and generates a `Module` class that calls all the generated DI classes.
118
+
119
+
```java
120
+
@Generated("io.avaje.inject.generator")
121
+
@InjectModule
122
+
publicfinalclassExampleModuleimplementsModule {
123
+
124
+
privateBuilder builder;
125
+
126
+
@Override
127
+
publicClass<?>[] classes() {
128
+
returnnew Class<?>[] {
129
+
org.example.DependencyClass.class,
130
+
org.example.DependencyClass2.class,
131
+
org.example.Example.class,
132
+
org.example.ExampleFactory.class,
133
+
};
134
+
}
135
+
136
+
/**
137
+
* Creates all the beans in order based on constructor dependencies. The beans are registered
138
+
* into the builder along with callbacks for field/method injection, and lifecycle
139
+
* support.
140
+
*/
141
+
@Override
142
+
publicvoidbuild(Builderbuilder) {
143
+
this.builder = builder;
144
+
// create beans in order based on constructor dependencies
0 commit comments