Skip to content
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

Update conditionalBeans.adoc to clarify how to depend on a conditional bean #10760

Merged
merged 3 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/docs/guide/ioc/conditionalBeans.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ At times, you may want a bean to load conditionally based on various potential f

The link:{api}/io/micronaut/context/annotation/Requires.html[Requires] annotation provides the ability to define one or many conditions on a bean.

If you declare an injection point, for example, `Bean a`, but no bean of type `Bean` can be instantiated, the framework throws a `NoSuchBeanException`.
The framework cannot inject a bean into an injection point if the bean requirements specified by `@Requires` are not fulfilled.

If you want to declare an injection point as Optional, you have several options:

* Mark the injection point as `@Nullable`. In Kotlin, nullable type names have `?` at the end; you don't need to specify the `@Nullable` annotation.
* Declare it as `BeanProvider<Bean>`. Learn about <<types, Injectable Container Types>>.

You can also inject a collection of beans. For example, you can inject `List<Bean>`. The collection is empty if no bean of type `Bean` fulfills the injection point.

=== @Requires Example

Consider the following example:

snippet::io.micronaut.docs.requires.JdbcBookService[tags="requires",indent=0, title="Using @Requires"]
Expand Down
Loading