Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
c736a169
Commit
c736a169
authored
Jun 02, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarify default value of `ConditionalOnMissingBean` on bean methods
Closes gh-9387
parent
b9a09fcd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
+53
-0
ConditionalOnBean.java
...ework/boot/autoconfigure/condition/ConditionalOnBean.java
+17
-0
ConditionalOnMissingBean.java
...oot/autoconfigure/condition/ConditionalOnMissingBean.java
+18
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+18
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.java
View file @
c736a169
...
...
@@ -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:
*
* <pre class="code">
* @Configuration
* public class MyAutoConfiguration {
*
* @ConditionalOnBean
* @Bean
* public MyService myService() {
* ...
* }
*
* }</pre>
* <p>
* In the sample above the condition will match if a bean of type {@code MyService} is
* already contained in the {@link BeanFactory}.
* <p>
* 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
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.java
View file @
c736a169
...
...
@@ -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}.
* <p>
* When placed on a {@code @Bean} method, the bean class default to the return type of
* the factory method:
*
* <pre class="code">
* @Configuration
* public class MyAutoConfiguration {
*
* @ConditionalOnMissingBean
* @Bean
* public MyService myService() {
* ...
* }
*
* }</pre>
* <p>
* In the sample above the condition will match if no bean of type {@code MyService} is
* already contained in the {@link BeanFactory}.
* <p>
* 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
...
...
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
c736a169
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment