Skip to content

Commit c736a16

Browse files
committed
Clarify default value of ConditionalOnMissingBean on bean methods
Closes gh-9387
1 parent b9a09fc commit c736a16

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@
3030
/**
3131
* {@link Conditional} that only matches when the specified bean classes and/or names are
3232
* already contained in the {@link BeanFactory}.
33+
* When placed on a {@code @Bean} method, the bean class default to the return type of
34+
* the factory method:
35+
*
36+
* <pre class="code">
37+
* &#064;Configuration
38+
* public class MyAutoConfiguration {
39+
*
40+
* &#064;ConditionalOnBean
41+
* &#064;Bean
42+
* public MyService myService() {
43+
* ...
44+
* }
45+
*
46+
* }</pre>
47+
* <p>
48+
* In the sample above the condition will match if a bean of type {@code MyService} is
49+
* already contained in the {@link BeanFactory}.
3350
* <p>
3451
* The condition can only match the bean definitions that have been processed by the
3552
* application context so far and, as such, it is strongly recommended to use this

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@
3131
* {@link Conditional} that only matches when the specified bean classes and/or names are
3232
* not already contained in the {@link BeanFactory}.
3333
* <p>
34+
* When placed on a {@code @Bean} method, the bean class default to the return type of
35+
* the factory method:
36+
*
37+
* <pre class="code">
38+
* &#064;Configuration
39+
* public class MyAutoConfiguration {
40+
*
41+
* &#064;ConditionalOnMissingBean
42+
* &#064;Bean
43+
* public MyService myService() {
44+
* ...
45+
* }
46+
*
47+
* }</pre>
48+
* <p>
49+
* In the sample above the condition will match if no bean of type {@code MyService} is
50+
* already contained in the {@link BeanFactory}.
51+
* <p>
3452
* The condition can only match the bean definitions that have been processed by the
3553
* application context so far and, as such, it is strongly recommended to use this
3654
* condition on auto-configuration classes only. If a candidate bean may be created by

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6231,6 +6231,24 @@ attribute to specify beans by type, or `name` to specify beans by name. The `sea
62316231
attribute allows you to limit the `ApplicationContext` hierarchy that should be considered
62326232
when searching for beans.
62336233

6234+
When placed on a `@Bean` method, the target type defaults to the return type of the
6235+
method, for instance:
6236+
6237+
[source,java,indent=0]
6238+
----
6239+
@Configuration
6240+
public class MyAutoConfiguration {
6241+
6242+
@Bean
6243+
@ConditionalOnMissingBean
6244+
public MyService myService() { ... }
6245+
6246+
}
6247+
----
6248+
6249+
In the example above, the `myService` bean is going to be created if no bean of type
6250+
`MyService` is already contained in the `ApplicationContext`.
6251+
62346252
TIP: You need to be very careful about the order that bean definitions are added as these
62356253
conditions are evaluated based on what has been processed so far. For this reason,
62366254
we recommend only using `@ConditionalOnBean` and `@ConditionalOnMissingBean` annotations

0 commit comments

Comments
 (0)