From c736a1698b9309b8f6b3376b9676bc000afa01d2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 2 Jun 2017 14:55:58 +0200 Subject: [PATCH] Clarify default value of `ConditionalOnMissingBean` on bean methods Closes gh-9387 --- .../condition/ConditionalOnBean.java | 17 +++++++++++++++++ .../condition/ConditionalOnMissingBean.java | 18 ++++++++++++++++++ .../main/asciidoc/spring-boot-features.adoc | 18 ++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.java index 9b33f74e55d..66beaf1cfde 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.java @@ -30,6 +30,23 @@ import org.springframework.context.annotation.Conditional; /** * {@link Conditional} that only matches when the specified bean classes and/or names are * already contained in the {@link BeanFactory}. + * When placed on a {@code @Bean} method, the bean class default to the return type of + * the factory method: + * + *
+ * @Configuration
+ * public class MyAutoConfiguration {
+ *
+ *     @ConditionalOnBean
+ *     @Bean
+ *     public MyService myService() {
+ *         ...
+ *     }
+ *
+ * }
+ *

+ * In the sample above the condition will match if a bean of type {@code MyService} is + * already contained in the {@link BeanFactory}. *

* The condition can only match the bean definitions that have been processed by the * application context so far and, as such, it is strongly recommended to use this diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.java index 6e19649ae5b..b75aefc3a75 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.java @@ -31,6 +31,24 @@ import org.springframework.context.annotation.Conditional; * {@link Conditional} that only matches when the specified bean classes and/or names are * not already contained in the {@link BeanFactory}. *

+ * When placed on a {@code @Bean} method, the bean class default to the return type of + * the factory method: + * + *

+ * @Configuration
+ * public class MyAutoConfiguration {
+ *
+ *     @ConditionalOnMissingBean
+ *     @Bean
+ *     public MyService myService() {
+ *         ...
+ *     }
+ *
+ * }
+ *

+ * In the sample above the condition will match if no bean of type {@code MyService} is + * already contained in the {@link BeanFactory}. + *

* The condition can only match the bean definitions that have been processed by the * application context so far and, as such, it is strongly recommended to use this * condition on auto-configuration classes only. If a candidate bean may be created by diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 52c2bbeb3fe..3d9baf64ac8 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -6231,6 +6231,24 @@ attribute to specify beans by type, or `name` to specify beans by name. The `sea attribute allows you to limit the `ApplicationContext` hierarchy that should be considered when searching for beans. +When placed on a `@Bean` method, the target type defaults to the return type of the +method, for instance: + +[source,java,indent=0] +---- + @Configuration + public class MyAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public MyService myService() { ... } + + } +---- + +In the example above, the `myService` bean is going to be created if no bean of type +`MyService` is already contained in the `ApplicationContext`. + TIP: You need to be very careful about the order that bean definitions are added as these conditions are evaluated based on what has been processed so far. For this reason, we recommend only using `@ConditionalOnBean` and `@ConditionalOnMissingBean` annotations