Skip to content

Helidon Inject documentation #9742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 27, 2025
Merged

Conversation

Verdent
Copy link
Member

@Verdent Verdent commented Feb 6, 2025

Description

Fixes #9741

Documentation

Adds user documentation for Helidon Inject

@Verdent Verdent self-assigned this Feb 6, 2025
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Feb 6, 2025

== Overview

Injection is the basic building stone for inversion of control. Dependency injection provides a mechanism to get
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend we start with the basics of Injection and IoC. SE developers, prior to 4.2 would be focused only on procedural coding thus a very brief introduction to what is Injection, what IoC is, why use Injection; why consider adopting IoC will help users. Include why we didn't just adopt CDI (light-weight, smaller, faster, easier to understand, ...) I also think we should describe the goals achieved with Helidon Inject (How/Why is Helidon Inject different from other Injection frameworks that a reader might have used/heard about. Some potential differentiators: -- compile-time generation (immutable images, more secure)-- source-code, not byte-code (easier debugging/stack traces) ... (add more beneficial facets as you think is appropriate).
Give the reader a bit of a primer on Inject, IoC, and set up reason to read further.

@tomas-langer tomas-langer self-requested a review February 18, 2025 10:09
Copy link
Member

@tomas-langer tomas-langer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of examples (injection.adoc will come later).

If you want to use programmatic lookup, there are two main ways to access a `ServiceRegistry` instance:

- Create a new ServiceRegistry instance and search for the desired service.
- Inject the existing ServiceRegistry instance currently used by Helidon and retrieve services from it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be used for advanced use cases, as it bypasses a lot of optimizations of the service registry (i.e. build time binding of services to injection points when using the Maven plugin)

== Startup

Helidon provides a Maven plugin (`io.helidon.service:helidon-service-maven-plugin`, goal `create-application`) to generate
build time bindings, that can be used to start the service registry without any classpath discovery and reflection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApplicationBinding provides build time bindings of service providers to injection points - this is to avoid lookup at runtime.
It does NOT elimiminate classpath discovery and reflection. To avoid that you need to use the generated ApplicationMain class.

Also classpath discovery does not mean classpath scanning (it sounds a bit bad in here) - we lookup all service.registry and service.loader resources from the classpath - we are not scanning for them (i.e. using only Java features that do not depend on how the classpath is created, so it works in native image as well)

Not sure how much of this should be in the docs, but it seems a bit misleading right now.

@Verdent Verdent force-pushed the inject-documentation branch from cf603dd to b27b204 Compare February 26, 2025 11:45
//Injection
[CARD]
.Injection
[icon=message,link=injection.adoc]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reference to injection document is in the "overview" of Helidon SE. Maybe we need to add a separate node in the SE tree

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... yes, I did not do this yet :-)

but aligns with concepts like an HTTP request-response cycle or message consumption in a messaging system.

== Build time
To ensure everything works correctly,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to explain first why we use build time approach and what are the benefits, and then mention what needs to be done.
Or prepare the user for "build time" somewhere in the overview above.


=== RunLevel
link:{service-registry-base-url}/io/helidon/service/registry/Service.RunLevel.html[`@Service.RunLevel`]
is the annotation used for specifying the order in which certain services should be created.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use "order .. be created".
The run level provides steps in which an application is started, and within each step (i.e. @Service.RunLevel(10)), you may have zero to n services to be started.
The order of creation is defined by @Weight; Run level creates "groups" of services.

include::{sourcedir}/se/inject/RunLevelExample.java[tag=snippet_1, indent=0]
----

For better understanding, we can also add helpful method, which get executed upon service construction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably obsoleted by switching to start method.

@Verdent Verdent merged commit a7c477c into helidon-io:main Feb 27, 2025
62 checks passed
@Verdent Verdent deleted the inject-documentation branch February 27, 2025 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Helidon Inject documentation
3 participants