Skip to content

Commit 5e5b7e2

Browse files
committed
Merge branch '1.5.x'
2 parents a6c01d3 + e94f213 commit 5e5b7e2

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6417,10 +6417,28 @@ attribute to specify beans by type, or `name` to specify beans by name. The `sea
64176417
attribute allows you to limit the `ApplicationContext` hierarchy that should be considered
64186418
when searching for beans.
64196419

6420+
When placed on a `@Bean` method, the target type defaults to the return type of the
6421+
method, for instance:
6422+
6423+
[source,java,indent=0]
6424+
----
6425+
@Configuration
6426+
public class MyAutoConfiguration {
6427+
6428+
@Bean
6429+
@ConditionalOnMissingBean
6430+
public MyService myService() { ... }
6431+
6432+
}
6433+
----
6434+
6435+
In the example above, the `myService` bean is going to be created if no bean of type
6436+
`MyService` is already contained in the `ApplicationContext`.
6437+
64206438
TIP: You need to be very careful about the order that bean definitions are added as these
64216439
conditions are evaluated based on what has been processed so far. For this reason,
64226440
we recommend only using `@ConditionalOnBean` and `@ConditionalOnMissingBean` annotations
6423-
on auto-configuration classes (since these are guaranteed to load after any user-define
6441+
on auto-configuration classes (since these are guaranteed to load after any user-defined
64246442
beans definitions have been added).
64256443

64266444
NOTE: `@ConditionalOnBean` and `@ConditionalOnMissingBean` do not prevent `@Configuration`

0 commit comments

Comments
 (0)