From 2f6edecedba0d6e6faeba9dca19d45817581a7dc Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 23 Nov 2022 16:26:43 +0100 Subject: [PATCH] Improve JavaDoc on ConditionalOnClass Closes gh-27846 --- .../condition/ConditionalOnClass.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java index 1aa0900d80..34adb36070 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java @@ -30,15 +30,12 @@ import org.springframework.context.annotation.Conditional; *

* A {@code Class} {@link #value() value} can be safely specified on * {@code @Configuration} classes as the annotation metadata is parsed by using ASM before - * the class is loaded. If a class reference cannot be used then a {@link #name() name} - * {@code String} attribute can be used. - *

- * Note: Extra care must be taken when using {@code @ConditionalOnClass} on - * {@code @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 {@code @Configuration} class should be used to isolate - * the condition. For example:

+ * the class is loaded. This only holds true if {@code @ConditionalOnClass} is used on a
+ * class. Extra care must be taken when using {@code @ConditionalOnClass} on {@code @Bean}
+ * methods: the {@link #value() value} attribute must not be used, instead the
+ * {@link #name() name} attribute can be used to reference the class which must be present
+ * as a {@code String}. Alternatively create a separate {@code @Configuration} class that
+ * isolates the condition. For example: 
  * @Configuration(proxyBeanMethods = false)
  * public class MyAutoConfiguration {
  *
@@ -66,17 +63,21 @@ import org.springframework.context.annotation.Conditional;
 public @interface ConditionalOnClass {
 
 	/**
-	 * The classes that must be present. Since this annotation is parsed by loading class
-	 * bytecode, it is safe to specify classes here that may ultimately not be on the
-	 * classpath, only if this annotation is directly on the affected component and
-	 * not if this annotation is used as a composed, meta-annotation. In order to
-	 * use this annotation as a meta-annotation, only use the {@link #name} attribute.
+	 * The classes that must be present. Using this attribute is safe when using
+	 * {@code ConditionalOnClass} at class level, but it must not be used when using
+	 * {@code ConditionalOnClass} on a {@code @Bean} method.
+	 * 

+ * Since this annotation is parsed by loading class bytecode, it is safe to specify + * classes here that may ultimately not be on the classpath, only if this annotation + * is directly on the affected component and not if this annotation is used as + * a composed, meta-annotation. In order to use this annotation as a meta-annotation, + * only use the {@link #name} attribute. * @return the classes that must be present */ Class[] value() default {}; /** - * The classes names that must be present. + * The class names that must be present. * @return the class names that must be present. */ String[] name() default {};