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
Copy file name to clipboardExpand all lines: docs/src/main/asciidoc/se/injection.adoc
+23-5Lines changed: 23 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,16 @@ Let's begin by explaining some basic terms.
71
71
72
72
== Basic terms
73
73
74
+
=== Dependency Injection
75
+
Injection is a way to automatically provide instances of dependencies without having to create them manually.
76
+
Instead of a class creating an object itself,
77
+
something else (like a <<Service registry,service registry>>) hands it over when needed.
78
+
This makes code cleaner, easier to manage, and more flexible.
79
+
80
+
For example, if a Car needs an Engine, instead of the Car making an Engine itself, it just asks for one,
81
+
and the system provides it.
82
+
This is called Dependency Injection (DI).
83
+
74
84
=== Declarative style of programming
75
85
In a declarative approach, you use annotations on classes, constructors, and constructor arguments to express your intent.
76
86
@@ -135,6 +145,9 @@ Services are defined by:
135
145
1. Java classes annotated with one of the `Service.Scope` annotations (see <<Scopes, Scopes>>)
136
146
2. Any class with `@Service.Inject` annotation even when it doesn’t have a scope annotation. In such a case, the scope of the service will be set as `@Service.PerLookup`.
137
147
148
+
Keep in mind that if you create any service instance directly, it will not get its injection points resolved!
149
+
This works only when using service registry.
150
+
138
151
Now, let's talk about an injection points.
139
152
140
153
== Injection points
@@ -167,7 +180,7 @@ Helidon Inject provides three built-in scopes:
167
180
- `@Service.PerRequest` – A single instance per request exists in the service registry. The definition of a "request" is not enforced by the injection framework but aligns with concepts like an HTTP request-response cycle or message consumption in a messaging system.
168
181
169
182
== Build time
170
-
To ensure everything functions correctly,
183
+
To ensure everything works correctly,
171
184
you need to add the following annotation processors to your application's compilation process.
172
185
173
186
These processors generate the necessary metadata and wiring for dependency injection and service registration in Helidon Inject.
@@ -232,7 +245,10 @@ to print out `Hello David!`. To find out more about this manual approach, please
If everything went as expected, no problems occurred and Service registry gave us fully initialized
248
+
The last step is ensuring that everything necessary for your application to compile correctly with injection is included.
249
+
See <<Build time,Build time>>.
250
+
251
+
If everything went as expected, no problems occurred and a Service registry gave us fully initialized
236
252
and ready to use service.
237
253
238
254
== Service Lifecycle
@@ -443,8 +459,6 @@ The `@Interception.Delegate` annotation enables interception for classes
443
459
that aren’t created through the service registry
444
460
but are instead produced by a factory (More about factories can be found here -
445
461
<<Factories, Factory chapter>>).
446
-
To enable interception, this annotation must be present on the class that the factory produces.
447
-
While it is not required on interfaces, it will still function correctly if applied there.
448
462
449
463
Let's make the same `@Traced` annotation and Interceptor as in the previous examples
450
464
@@ -463,13 +477,17 @@ include::{sourcedir}/se/inject/InterceptorDelegateExample.java[tag=snippet_2, in
463
477
----
464
478
465
479
Method calls on an instance created this way can’t be intercepted. To enable interception in such cases, we use the `@Interception.Delegate` annotation. However, keep in mind that usage of this annotation doesn’t add the ability to intercept constructor calls.
480
+
To enable interception, this annotation must be present on the class that the factory produces.
481
+
While it is not required on interfaces, it will still work correctly if applied there.
466
482
467
-
If you need to enable interception for classes using delegation, you should:
483
+
If you need to enable interception for classes using delegation, you should make sure about the following:
468
484
469
485
- The class must have accessible no-arg constructor (at least package local)
470
486
- The constructor should have no side effects, as the instance will act only as a wrapper for the delegate
471
487
- All invoked methods must be accessible (at least package local)
472
488
489
+
Therefore using `@Interception.Delegate` on the classes can be considered as acknowledgement of the rules above.
0 commit comments