Polish class conditions documentation
Closes gh-15578
This commit is contained in:
@@ -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]
|
||||
====
|
||||
|
||||
Reference in New Issue
Block a user