Skip to content

Commit 4ca7f58

Browse files
committed
Javadoc improvements
1 parent b01b35f commit 4ca7f58

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

inject/src/main/java/io/avaje/inject/package-info.java

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
/**
22
* Avaje Inject API - see {@link io.avaje.inject.BeanScope}.
3+
*
4+
* <h2>Overview</h2>
5+
*
6+
* <h4>1. @Singleton / @Factory</h4>
7+
* <p>
8+
* Put <code>@Singleton</code> on beans that we want avaje-inject to wire.
9+
* <p>
10+
* We can use <code>@Factory/@Bean</code> to programmatically create dependencies
11+
* when they have interesting construction logic (e.g. construction depends on
12+
* system/environment properties etc - like Spring @Configuration).
13+
*
14+
* <pre>{@code
15+
*
16+
* @Singleton
17+
* public class CoffeeMaker { ... }
18+
*
19+
* @Singleton
20+
* public class Pump { ... }
21+
*
22+
* // Use @Factory to programmatically build dependencies
23+
*
24+
* @Factory
25+
* public class MyFactory {
26+
*
27+
* @Bean
28+
* Grinder buildGrinder(Pump pump) { // interesting construction logic ... }
29+
*
30+
* }
31+
*
32+
* }</pre>
33+
*
34+
* <h4>2. Create and use BeanScope</h4>
335
* <p>
436
* Create {@link io.avaje.inject.BeanScope} using a builder.
537
* Obtain beans from the scope and use them.
@@ -10,15 +42,118 @@
1042
*
1143
* <pre>{@code
1244
*
45+
* // create BeanScope
1346
* BeanScope scope = BeanScope.newBuilder()
1447
* .build();
1548
*
49+
* // use it
1650
* CoffeeMaker coffeeMaker = scope.get(CoffeeMaker.class);
1751
* coffeeMaker.makeIt();
1852
*
19-
* // fire preDestroy lifecycle methods
53+
* // close it to fire preDestroy lifecycle methods
2054
* scope.close();
2155
*
2256
* }</pre>
57+
*
58+
* <h2>Default scope</h3>
59+
* <p>
60+
* All beans annotated with {@code @Singleton} and {@code @Factory} are by default included in
61+
* the "default scope".
62+
* <p>
63+
* When we create a BeanScope and do not specify any modules then all the beans in the default
64+
* scope are included. This is done via ServiceLoader and includes all 'default scope' modules
65+
* that are in the classpath (other jars can have default scope modules and these are all included).
66+
*
67+
* <h4>Generated code</h4>
68+
* <p>
69+
* <em>avaje-inject</em> will generate a <code>$DI</code> class for each bean that it is going to
70+
* wire - this has the code that instantiates the bean, performs field and method injection and
71+
* lifecycle support (PostConstruct and PreDestroy).
72+
* <p>
73+
* <em>avaje-inject</em> will generate a Module class for the default scope. The main job of
74+
* the module code is to specify the ordering of how all the beans are instantiated.
75+
* <p>
76+
* We typically find the generated source code in <em>target/generate-sources/annotations</em>.
77+
*
78+
*
79+
* <h2>Custom scopes</h3>
80+
* <p>
81+
* We can create custom scopes that only contain the beans/components that we want to include in
82+
* that scope. These beans/components in the custom scope are not included in the default scope.
83+
* <p>
84+
* To do this we:
85+
*
86+
* <h4>1. Create the scope annotation</h4>
87+
* <p>
88+
* Create an annotation that has the {@code Scope} annotation. In the example below we create the
89+
* <code>@StoreComponent</code> annotation. We will put this annotation on beans that we want
90+
* included in the scope.
91+
*
92+
* <pre>{@code
93+
*
94+
* @Scope
95+
* public @interface StoreComponent {
96+
* }
97+
*
98+
* }</pre>
99+
*
100+
* <p>
101+
* Note that if this scope depends on external dependencies or another scope we specify that via
102+
* {@code @InjectModule requires}. In the example below our StoreComponent depends on another
103+
* scope called QueueComponent and an external dependency - SomeExternalDependency.
104+
*
105+
* <pre>{@code
106+
*
107+
* @Scope
108+
* @InjectModule(requires = {QueueComponent.class, SomeExternalDependency.class})
109+
* public @interface StoreComponent {
110+
* }
111+
*
112+
* }</pre>
113+
*
114+
* <h4>2. Use the annotation</h4>
115+
* <p>
116+
* Put the <code>@StoreComponent</code> annotation on the beans that we want included in the
117+
* custom scope.
118+
*
119+
* <pre>{@code
120+
*
121+
* @StoreComponent
122+
* public class StoreLoader {
123+
* ...
124+
* }
125+
*
126+
* }</pre>
127+
*
128+
*
129+
* <h4>3. Generated Module</h4>
130+
* <p>
131+
* <em>avaje-inject</em> will generate a <code>StoreComponentModule</code> with the appropriate
132+
* code to create/wire all the beans in the custom scope.
133+
* <p>
134+
* StoreComponentModule is typically found in <em>target/generate-sources/annotations</em>.
135+
* For each component we will also see a <code>$DI</code> class which is the generated code
136+
* that creates the component.
137+
*
138+
* <h4>4. Use the custom scope</h4>
139+
* <p>
140+
* To use the custom scope we specify the <code>StoreComponentModule</code> when creating the
141+
* BeanScope. Only the components in our module will be create/wired into the BeanScope.
142+
* <p>
143+
* If the scope depends on another scope then we specify that using {@code withParent()}.
144+
*
145+
* <pre>{@code
146+
*
147+
* BeanScope parentScope = ...
148+
*
149+
* BeanScope scope = BeanScope.newBuilder()
150+
* .withModules(new StoreComponentModule())
151+
* .withParent(parentScope)
152+
* .build());
153+
*
154+
* StoreLoader storeLoader = scope.get(StoreLoader.class);
155+
* storeLoader.load();
156+
*
157+
* }</pre>
23158
*/
24159
package io.avaje.inject;

0 commit comments

Comments
 (0)