Skip to content

Commit c6202b5

Browse files
committed
Merge branch '2.0.x' into 2.1.x
2 parents a8efcad + be161b2 commit c6202b5

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,12 @@
2626

2727
/**
2828
* {@link Conditional} that only matches when the specified classes are on the classpath.
29+
* <p>
30+
* A {@link #value()} can be safely specified on {@code @Configuration} classes as the
31+
* annotation metadata is parsed by using ASM before the class is loaded. Extra care is
32+
* required when placed on {@code @Bean} methods, consider isolating the condition in a
33+
* separate {@code Configuration} class, in particular if the return type of the method
34+
* matches the {@link #value target of the condition}.
2935
*
3036
* @author Phillip Webb
3137
*/

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8047,12 +8047,41 @@ annotations include:
80478047

80488048
[[boot-features-class-conditions]]
80498049
==== Class Conditions
8050-
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let configuration
8051-
be included based on the presence or absence of specific classes. Due to the fact that
8052-
annotation metadata is parsed by using http://asm.ow2.org/[ASM], you can use the `value`
8053-
attribute to refer to the real class, even though that class might not actually appear on
8054-
the running application classpath. You can also use the `name` attribute if you prefer to
8055-
specify the class name by using a `String` value.
8050+
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let
8051+
`@Configuration` classes be included based on the presence or absence of specific classes.
8052+
Due to the fact that annotation metadata is parsed by using http://asm.ow2.org/[ASM], you
8053+
can use the `value` attribute to refer to the real class, even though that class might not
8054+
actually appear on the running application classpath. You can also use the `name`
8055+
attribute if you prefer to specify the class name by using a `String` value.
8056+
8057+
This mechanism does not apply the same way to `@Bean` methods where typically the return
8058+
type is the target of the condition: before the condition on the method applies, the JVM
8059+
will have loaded the class and potentially processed method references which will fail if
8060+
the class is not present.
8061+
8062+
To handle this scenario, a separate `@Configuration` class can be used to isolate the
8063+
condition, as shown in the following example:
8064+
8065+
[source,java,indent=0]
8066+
----
8067+
@Configuration
8068+
// Some conditions
8069+
public class MyAutoConfiguration {
8070+
8071+
// Auto-configured beans
8072+
8073+
@Configuration
8074+
@ConditionalOnClass(EmbeddedAcmeService.class)
8075+
static class EmbeddedConfiguration {
8076+
8077+
@Bean
8078+
@ConditionalOnMissingBean
8079+
public EmbeddedAcmeService embeddedAcmeService() { ... }
8080+
8081+
}
8082+
8083+
}
8084+
----
80568085

80578086
[TIP]
80588087
====

0 commit comments

Comments
 (0)