From fce3cd1684cc0c581f904ec4e3cd6aaacb8126a2 Mon Sep 17 00:00:00 2001 From: Mihail Cornescu Date: Wed, 5 Jan 2022 18:03:29 +0000 Subject: [PATCH 1/2] Add a note on early init with @ConditionalOnExpression See gh-29276 --- .../boot/autoconfigure/condition/ConditionalOnExpression.java | 4 ++++ .../docs/asciidoc/features/developing-auto-configuration.adoc | 1 + 2 files changed, 5 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java index bdef69f3b7..88547a7e16 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java @@ -40,6 +40,10 @@ public @interface ConditionalOnExpression { /** * The SpEL expression to evaluate. Expression should return {@code true} if the * condition passes or {@code false} if it fails. + *

+ * NOTE: Referencing a bean in the expression will cause that bean to be initialized + * very early in context refresh processing. As a result, the bean will load with + * default property values. * @return the SpEL expression */ String value() default "true"; diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc index df245f4ee0..2fdbc2c0e2 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc @@ -143,6 +143,7 @@ This condition will not match for applications that are run with an embedded ser ==== SpEL Expression Conditions The `@ConditionalOnExpression` annotation lets configuration be included based on the result of a {spring-framework-docs}/core.html#expressions[SpEL expression]. +NOTE: Referencing a bean in the expression will cause that bean to be initialized very early in context refresh processing. As a result, the bean will load with default property values. [[features.developing-auto-configuration.testing]] From 5fe617696619608397306c7ce7725bc092a7007e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 1 Feb 2022 16:32:27 +0100 Subject: [PATCH 2/2] Polish "Add a note on early init with @ConditionalOnExpression" See gh-29276 --- .../condition/ConditionalOnExpression.java | 11 ++++++----- .../features/developing-auto-configuration.adoc | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java index 88547a7e16..a8c4782a5b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnExpression.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 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. @@ -27,6 +27,11 @@ import org.springframework.context.annotation.Conditional; /** * Configuration annotation for a conditional element that depends on the value of a SpEL * expression. + *

+ * Referencing a bean in the expression will cause that bean to be initialized very early + * in context refresh processing. As a result, the bean won't be eligible for + * post-processing (such as configuration properties binding) and its state may be + * incomplete. * * @author Dave Syer * @since 1.0.0 @@ -40,10 +45,6 @@ public @interface ConditionalOnExpression { /** * The SpEL expression to evaluate. Expression should return {@code true} if the * condition passes or {@code false} if it fails. - *

- * NOTE: Referencing a bean in the expression will cause that bean to be initialized - * very early in context refresh processing. As a result, the bean will load with - * default property values. * @return the SpEL expression */ String value() default "true"; diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc index 2fdbc2c0e2..afd1f457f2 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc @@ -143,7 +143,9 @@ This condition will not match for applications that are run with an embedded ser ==== SpEL Expression Conditions The `@ConditionalOnExpression` annotation lets configuration be included based on the result of a {spring-framework-docs}/core.html#expressions[SpEL expression]. -NOTE: Referencing a bean in the expression will cause that bean to be initialized very early in context refresh processing. As a result, the bean will load with default property values. +NOTE: Referencing a bean in the expression will cause that bean to be initialized very early in context refresh processing. +As a result, the bean won't be eligible for post-processing (such as configuration properties binding) and its state may be incomplete. + [[features.developing-auto-configuration.testing]]