Skip to content

Commit

Permalink
Update conditionalBeans.adoc to clarify how to depend on a conditiona…
Browse files Browse the repository at this point in the history
…l bean (#10760)
  • Loading branch information
mikehearn committed May 4, 2024
1 parent 8fe5365 commit 2bd917d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/docs/guide/ioc/conditionalBeans.adoc
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

0 comments on commit 2bd917d

Please sign in to comment.