Skip to content

Commit be161b2

Browse files
committed
Polish class conditions documentation
Closes spring-projectsgh-15578
1 parent 0a05688 commit be161b2

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

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

+7-1
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
*/

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

+35-6
Original file line numberDiff line numberDiff line change
@@ -7606,12 +7606,41 @@ annotations include:
76067606

76077607
[[boot-features-class-conditions]]
76087608
==== Class Conditions
7609-
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let configuration
7610-
be included based on the presence or absence of specific classes. Due to the fact that
7611-
annotation metadata is parsed by using http://asm.ow2.org/[ASM], you can use the `value`
7612-
attribute to refer to the real class, even though that class might not actually appear on
7613-
the running application classpath. You can also use the `name` attribute if you prefer to
7614-
specify the class name by using a `String` value.
7609+
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let
7610+
`@Configuration` classes be included based on the presence or absence of specific classes.
7611+
Due to the fact that annotation metadata is parsed by using http://asm.ow2.org/[ASM], you
7612+
can use the `value` attribute to refer to the real class, even though that class might not
7613+
actually appear on the running application classpath. You can also use the `name`
7614+
attribute if you prefer to specify the class name by using a `String` value.
7615+
7616+
This mechanism does not apply the same way to `@Bean` methods where typically the return
7617+
type is the target of the condition: before the condition on the method applies, the JVM
7618+
will have loaded the class and potentially processed method references which will fail if
7619+
the class is not present.
7620+
7621+
To handle this scenario, a separate `@Configuration` class can be used to isolate the
7622+
condition, as shown in the following example:
7623+
7624+
[source,java,indent=0]
7625+
----
7626+
@Configuration
7627+
// Some conditions
7628+
public class MyAutoConfiguration {
7629+
7630+
// Auto-configured beans
7631+
7632+
@Configuration
7633+
@ConditionalOnClass(EmbeddedAcmeService.class)
7634+
static class EmbeddedConfiguration {
7635+
7636+
@Bean
7637+
@ConditionalOnMissingBean
7638+
public EmbeddedAcmeService embeddedAcmeService() { ... }
7639+
7640+
}
7641+
7642+
}
7643+
----
76157644

76167645
[TIP]
76177646
====

0 commit comments

Comments
 (0)