-
Notifications
You must be signed in to change notification settings - Fork 593
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
+1,593
−0
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dce5ae0
inject documentation first
Verdent f186a77
lookup and start chapters added
Verdent 4939e1b
changed the title level structure
Verdent 478f164
minor improvements to not finished parts and currently skipped ones
Verdent 5e7f5ea
copyright added
Verdent c3d2f44
Factory types clarified
Verdent 91531fd
minor adjustments
Verdent dd3b00f
another improvements
Verdent fdd1c93
minor adjustments
Verdent 8686e42
some changes from the monday meeting added
Verdent dbc4346
Minor adjustments and wording updates
Verdent 115582f
The first part of example suggestions implemented
Verdent 6381a01
Minor factory example changed and javadoc links added
Verdent b27b204
Suggestions implemented
Verdent bc0562b
The first batch of changes added
Verdent 9a49130
Second batch of changes
Verdent d53cb2a
another set of changes
Verdent 9a8ad01
another set of changes
Verdent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
docs/src/main/java/io/helidon/docs/se/inject/BasicExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright (c) 2025 Oracle and/or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.helidon.docs.se.inject; | ||
|
|
||
| import io.helidon.service.registry.Service; | ||
| import io.helidon.service.registry.ServiceRegistryManager; | ||
|
|
||
| class BasicExample { | ||
|
|
||
| // tag::snippet_1[] | ||
| @Service.Singleton | ||
| class Greeter { | ||
|
|
||
| String greet(String name) { | ||
| return "Hello %s!".formatted(name); | ||
| } | ||
|
|
||
| } | ||
| // end::snippet_1[] | ||
|
|
||
| // tag::snippet_2[] | ||
| @Service.Singleton | ||
| class GreetingInjectionService { | ||
|
|
||
| private final Greeter greeter; | ||
|
|
||
| @Service.Inject | ||
| GreetingInjectionService(Greeter greeter) { | ||
| this.greeter = greeter; | ||
| } | ||
|
|
||
| void printGreeting(String name) { | ||
| System.out.println(greeter.greet(name)); | ||
| } | ||
| } | ||
| // end::snippet_2[] | ||
|
|
||
| // tag::snippet_3[] | ||
| public static void main(String[] args) { | ||
| var serviceRegistryManager = ServiceRegistryManager.create(); | ||
| var registry = serviceRegistryManager.registry(); | ||
| var greetings = registry.get(GreetingInjectionService.class); | ||
| greetings.printGreeting("David"); | ||
|
|
||
| serviceRegistryManager.shutdown(); //Once not needed, it should be shut down | ||
| } | ||
| // end::snippet_3[] | ||
|
|
||
| } |
102 changes: 102 additions & 0 deletions
102
docs/src/main/java/io/helidon/docs/se/inject/EventsExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright (c) 2025 Oracle and/or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.helidon.docs.se.inject; | ||
|
|
||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| import io.helidon.docs.se.inject.Qualifier2Example.Blue; | ||
| import io.helidon.service.registry.Event; | ||
| import io.helidon.service.registry.Service; | ||
|
|
||
| class EventsExample { | ||
|
|
||
| // tag::snippet_1[] | ||
| /** | ||
| * A custom event payload. | ||
| * @param msg message | ||
| */ | ||
| record MyEvent(String msg) { | ||
| } | ||
| // end::snippet_1[] | ||
|
|
||
| // tag::snippet_2[] | ||
| @Service.Singleton | ||
| record MyEventProducer(Event.Emitter<MyEvent> emitter) { | ||
|
|
||
| void emit(String msg) { | ||
| emitter.emit(new MyEvent(msg)); | ||
| } | ||
| } | ||
| // end::snippet_2[] | ||
|
|
||
|
|
||
| // tag::snippet_3[] | ||
| @Service.Singleton | ||
| class MyEventObserver { | ||
|
|
||
| @Event.Observer | ||
| void event(MyEvent event) { | ||
| //Do something with the event | ||
| } | ||
| } | ||
| // end::snippet_3[] | ||
|
|
||
|
|
||
| // tag::snippet_4[] | ||
| @Service.Singleton | ||
| record MyAsyncEmitter(Event.Emitter<MyEvent> emitter) { | ||
|
|
||
| void emit(String msg) { | ||
| CompletionStage<MyEvent> completionStage = emitter.emitAsync(new MyEvent(msg)); | ||
| //Do something with the completion stage | ||
| } | ||
| } | ||
| // end::snippet_4[] | ||
|
|
||
| // tag::snippet_5[] | ||
| @Service.Singleton | ||
| class MyEventAsyncObserver { | ||
|
|
||
| @Event.AsyncObserver | ||
| void event(MyEvent event) { | ||
| //Do something with the event | ||
| } | ||
| } | ||
| // end::snippet_5[] | ||
|
|
||
| // tag::snippet_6[] | ||
| @Service.Singleton | ||
| record MyBlueProducer(@Blue Event.Emitter<String> emitter) { | ||
|
|
||
| void emit(String msg) { | ||
| emitter.emit(msg); | ||
| } | ||
| } | ||
| // end::snippet_6[] | ||
|
|
||
| // tag::snippet_7[] | ||
| @Service.Singleton | ||
| class MyBlueObserver { | ||
|
|
||
| @Event.Observer | ||
| @Blue | ||
| void event(MyEvent event) { | ||
| //Do something with the event | ||
| } | ||
| } | ||
| // end::snippet_7[] | ||
|
|
||
| } |
118 changes: 118 additions & 0 deletions
118
docs/src/main/java/io/helidon/docs/se/inject/FactoryExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright (c) 2025 Oracle and/or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.helidon.docs.se.inject; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import io.helidon.common.GenericType; | ||
| import io.helidon.service.registry.Lookup; | ||
| import io.helidon.service.registry.Qualifier; | ||
| import io.helidon.service.registry.Service; | ||
|
|
||
| class FactoryExample { | ||
|
|
||
| record MyService() {} | ||
|
|
||
| // tag::snippet_1[] | ||
| /** | ||
| * Supplier service factory. | ||
| */ | ||
| @Service.Singleton | ||
| class MyServiceProvider implements Supplier<MyService> { | ||
|
|
||
| @Override | ||
| public MyService get() { | ||
| return new MyService(); | ||
| } | ||
| } | ||
| // end::snippet_1[] | ||
|
|
||
| // tag::snippet_2[] | ||
| @Service.Singleton | ||
| class MyServiceFactory implements Service.ServicesFactory<MyService> { | ||
Verdent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public List<Service.QualifiedInstance<MyService>> services() { | ||
| var named = Service.QualifiedInstance.create(new MyService(), Qualifier.createNamed("name")); | ||
| var named2 = Service.QualifiedInstance.create(new MyService(), Qualifier.createNamed("name2")); | ||
| return List.of(named, named2); | ||
| } | ||
| } | ||
| // end::snippet_2[] | ||
|
|
||
| // tag::snippet_3[] | ||
| @Service.Qualifier | ||
| @interface SystemProperty { | ||
| String value(); | ||
| } | ||
|
|
||
| @Service.Singleton | ||
| class SystemProperties { | ||
|
|
||
| private final String httpHost; | ||
| private final String httpPort; | ||
|
|
||
| SystemProperties(@SystemProperty("http.host") String httpHost, | ||
| @SystemProperty("http.port") String httpPort) { | ||
| this.httpHost = httpHost; | ||
| this.httpPort = httpPort; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Service.Singleton | ||
| class SystemPropertyFactory implements Service.QualifiedFactory<String, SystemProperty> { | ||
|
|
||
| @Override | ||
| public Optional<Service.QualifiedInstance<String>> first(Qualifier qualifier, | ||
| Lookup lookup, | ||
| GenericType<String> genericType) { | ||
| return qualifier.stringValue() | ||
| .map(System::getProperty) | ||
| .map(propertyValue -> Service.QualifiedInstance.create(propertyValue, qualifier)); | ||
| } | ||
|
|
||
| } | ||
| // end::snippet_3[] | ||
|
|
||
| // tag::snippet_4[] | ||
| @Service.Singleton | ||
| class TestClass { | ||
|
|
||
| private final System.Logger logger; | ||
|
|
||
| TestClass(System.Logger logger) { | ||
| this.logger = logger; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Service.Singleton | ||
| class LoggerFactory implements Service.InjectionPointFactory<System.Logger> { | ||
| private static final System.Logger DEFAULT_LOGGER = System.getLogger(LoggerFactory.class.getName()); | ||
|
|
||
| @Override | ||
| public Optional<Service.QualifiedInstance<System.Logger>> first(Lookup lookup) { | ||
| System.Logger logger = lookup.dependency() | ||
| .map(dep -> System.getLogger(dep.service().fqName())) | ||
| .orElse(DEFAULT_LOGGER); | ||
|
|
||
| return Optional.of(Service.QualifiedInstance.create(logger)); | ||
| } | ||
| } | ||
| // end::snippet_4[] | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :-)