mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Polish class conditions documentation
Closes gh-15578
This commit is contained in:
parent
0a0568867a
commit
be161b23cb
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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
|
||||
*/
|
||||
|
@ -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]
|
||||
====
|
||||
|
Loading…
Reference in New Issue
Block a user