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
be161b23
Commit
be161b23
authored
Dec 31, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish class conditions documentation
Closes gh-15578
parent
0a056886
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
7 deletions
+42
-7
ConditionalOnClass.java
...work/boot/autoconfigure/condition/ConditionalOnClass.java
+7
-1
spring-boot-features.adoc
...ing-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+35
-6
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java
View file @
be161b23
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -26,6 +26,12 @@ import org.springframework.context.annotation.Conditional;
/**
* {@link Conditional} that only matches when the specified classes are on the classpath.
* <p>
* A {@link #value()} can be safely specified on {@code @Configuration} classes as the
* annotation metadata is parsed by using ASM before the class is loaded. Extra care is
* required when placed on {@code @Bean} methods, consider isolating the condition in a
* separate {@code Configuration} class, in particular if the return type of the method
* matches the {@link #value target of the condition}.
*
* @author Phillip Webb
*/
...
...
spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
be161b23
...
...
@@ -7606,12 +7606,41 @@ annotations include:
[[boot-features-class-conditions]]
==== Class Conditions
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let configuration
be included based on the presence or absence of specific classes. Due to the fact that
annotation metadata is parsed by using http://asm.ow2.org/[ASM], you can use the `value`
attribute to refer to the real class, even though that class might not actually appear on
the running application classpath. You can also use the `name` attribute if you prefer to
specify the class name by using a `String` value.
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let
`@Configuration` classes be included based on the presence or absence of specific classes.
Due to the fact that annotation metadata is parsed by using http://asm.ow2.org/[ASM], you
can use the `value` attribute to refer to the real class, even though that class might not
actually appear on the running application classpath. You can also use the `name`
attribute if you prefer to specify the class name by using a `String` value.
This mechanism does not apply the same way to `@Bean` methods where typically the return
type is the target of the condition: before the condition on the method applies, the JVM
will have loaded the class and potentially processed method references which will fail if
the class is not present.
To handle this scenario, a separate `@Configuration` class can be used to isolate the
condition, as shown in the following example:
[source,java,indent=0]
----
@Configuration
// Some conditions
public class MyAutoConfiguration {
// Auto-configured beans
@Configuration
@ConditionalOnClass(EmbeddedAcmeService.class)
static class EmbeddedConfiguration {
@Bean
@ConditionalOnMissingBean
public EmbeddedAcmeService embeddedAcmeService() { ... }
}
}
----
[TIP]
====
...
...
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