From 0bdf7e8af770f0dc4995713cde20f9eefa30f78d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 7 Nov 2022 14:45:43 -0800 Subject: [PATCH] Allow spring properties to be used in logback blocks Reorder `SpringBootJoranConfigurator.addModelHandlerAssociations` so that handlers are added before calling the super method. Prior to this commit, handlers were added behind filters which prevented them from being used in `` blocks. Fixes gh-33028 --- .../spring-boot-parent/build.gradle | 7 +++++++ spring-boot-project/spring-boot/build.gradle | 1 + .../logback/SpringBootJoranConfigurator.java | 2 +- .../logback/SpringBootJoranConfiguratorTests.java | 14 ++++++++++++++ .../boot/logging/logback/property-in-if.xml | 10 ++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-in-if.xml diff --git a/spring-boot-project/spring-boot-parent/build.gradle b/spring-boot-project/spring-boot-parent/build.gradle index 1482eeea8d6..e0f264ad199 100644 --- a/spring-boot-project/spring-boot-parent/build.gradle +++ b/spring-boot-project/spring-boot-parent/build.gradle @@ -55,6 +55,13 @@ bom { ] } } + library("Janino", "3.1.8") { + group("org.codehaus.janino") { + imports = [ + "janino" + ] + } + } library("JLine", "2.11") { prohibit("[2.12,)") { because "it contains breaking changes" diff --git a/spring-boot-project/spring-boot/build.gradle b/spring-boot-project/spring-boot/build.gradle index 68c494dc415..9993334d2e6 100644 --- a/spring-boot-project/spring-boot/build.gradle +++ b/spring-boot-project/spring-boot/build.gradle @@ -115,6 +115,7 @@ dependencies { testImplementation("org.apache.derby:derby") testImplementation("org.apache.derby:derbytools") testImplementation("org.awaitility:awaitility") + testImplementation("org.codehaus.janino:janino") testImplementation("org.eclipse.jetty:jetty-client") testImplementation("org.eclipse.jetty.http2:http2-client") testImplementation("org.eclipse.jetty.http2:http2-http-client-transport") diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java index be1a949921a..b2a82e58645 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java @@ -84,13 +84,13 @@ class SpringBootJoranConfigurator extends JoranConfigurator { @Override protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) { - super.addModelHandlerAssociations(defaultProcessor); defaultProcessor.addHandler(SpringPropertyModel.class, (handlerContext, handlerMic) -> new SpringPropertyModelHandler(this.context, this.initializationContext.getEnvironment())); defaultProcessor.addHandler(SpringProfileModel.class, (handlerContext, handlerMic) -> new SpringProfileModelHandler(this.context, this.initializationContext.getEnvironment())); + super.addModelHandlerAssociations(defaultProcessor); } @Override diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java index 20b06e6662b..2dfb9fe3b59 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java @@ -189,6 +189,20 @@ class SpringBootJoranConfiguratorTests { assertThat(this.context.getProperty("MINE")).isEqualTo("bar"); } + @Test + void springPropertyInIfWhenTrue() throws Exception { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "my.example-property=true"); + initialize("property-in-if.xml"); + assertThat(this.context.getProperty("MYCHECK")).isEqualTo("i-was-included"); + } + + @Test + void springPropertyInIfWhenFalse() throws Exception { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "my.example-property=false"); + initialize("property-in-if.xml"); + assertThat(this.context.getProperty("MYCHECK")).isNull(); + } + @Test void addsAotContributionToContextDuringAotProcessing() throws Exception { withSystemProperty("spring.aot.processing", "true", () -> { diff --git a/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-in-if.xml b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-in-if.xml new file mode 100644 index 00000000000..b16fde51f3e --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-in-if.xml @@ -0,0 +1,10 @@ + + + + + + + + + +