From 31b1c3e8adfdeed746ced8bcf44bcc38c957bb94 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 22 Feb 2023 19:47:41 +0000 Subject: [PATCH] Replace property placeholders in Logback component class names Closes gh-34336 --- .../logback/SpringBootJoranConfigurator.java | 3 ++- ...backConfigurationAotContributionTests.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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 0fcc1e4c1b..c16a7d10b6 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 @@ -269,7 +269,8 @@ class SpringBootJoranConfigurator extends JoranConfigurator { private Class loadComponentType(String componentType) { try { - return ClassUtils.forName(componentType, getClass().getClassLoader()); + return ClassUtils.forName(this.modelInterpretationContext.subst(componentType), + getClass().getClassLoader()); } catch (Throwable ex) { throw new RuntimeException("Failed to load component type '" + componentType + "'", ex); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java index 5681a08019..4714728fd4 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Stream; @@ -190,6 +191,18 @@ class LogbackConfigurationAotContributionTests { .accepts(generationContext.getRuntimeHints()); } + @Test + void placeholdersInComponentClassAttributeAreReplaced() { + ComponentModel component = new ComponentModel(); + component.setClassName("${VARIABLE_CLASS_NAME}"); + TestGenerationContext generationContext = applyContribution(component, + (context) -> context.putProperty("VARIABLE_CLASS_NAME", Outer.class.getName())); + assertThat(invokePublicConstructorsAndInspectAndInvokePublicMethodsOf(Outer.class)) + .accepts(generationContext.getRuntimeHints()); + assertThat(invokePublicConstructorsAndInspectAndInvokePublicMethodsOf(Implementation.class)) + .accepts(generationContext.getRuntimeHints()); + } + private Predicate invokePublicConstructorsOf(String name) { return RuntimeHintsPredicates.reflection() .onType(TypeReference.of(name)) @@ -220,7 +233,13 @@ class LogbackConfigurationAotContributionTests { } private TestGenerationContext applyContribution(Model model) { + return this.applyContribution(model, (context) -> { + }); + } + + private TestGenerationContext applyContribution(Model model, Consumer contextCustomizer) { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + contextCustomizer.accept(context); SpringBootJoranConfigurator configurator = new SpringBootJoranConfigurator(null); configurator.setContext(context); withSystemProperty("spring.aot.processing", "true", () -> configurator.processModel(model));