From 36e31c06120367fc324482421decfc47284691ce Mon Sep 17 00:00:00 2001 From: Marc Becker Date: Wed, 2 Aug 2023 13:30:12 +0000 Subject: [PATCH] Add resource hints for MessageSource This only registers the default locations, not the one users can provide via 'spring.messages.basename'. This is similar to the approach taken for schema.sql and data.sql in class SqlInitializationScriptsRuntimeHints. See gh-36682 --- .../context/MessageSourceAutoConfiguration.java | 15 +++++++++++++++ .../MessageSourceAutoConfigurationTests.java | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java index ed306db9882..0ec92b6568b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java @@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.context; import java.time.Duration; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -26,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration.MessageSourceRuntimeHints; import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration.ResourceBundleCondition; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -33,6 +36,7 @@ import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.core.Ordered; @@ -48,6 +52,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer * @author Phillip Webb * @author Eddú Meléndez + * @author Marc Becker * @since 1.5.0 */ @AutoConfiguration @@ -55,6 +60,7 @@ import org.springframework.util.StringUtils; @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Conditional(ResourceBundleCondition.class) @EnableConfigurationProperties +@ImportRuntimeHints(MessageSourceRuntimeHints.class) public class MessageSourceAutoConfiguration { private static final Resource[] NO_RESOURCES = {}; @@ -125,4 +131,13 @@ public class MessageSourceAutoConfiguration { } + static class MessageSourceRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.resources().registerPattern("messages.properties").registerPattern("messages_*.properties"); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java index 313624d8779..e3098316413 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java @@ -21,7 +21,10 @@ import java.util.Locale; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration.MessageSourceRuntimeHints; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; @@ -40,6 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Eddú Meléndez * @author Stephane Nicoll * @author Kedar Joshi + * @author Marc Becker */ class MessageSourceAutoConfigurationTests { @@ -180,6 +184,15 @@ class MessageSourceAutoConfigurationTests { .run((context) -> assertThat(context.getMessage("foo", null, Locale.US)).isEqualTo("bar")); } + @Test + void shouldRegisterDefaultHints() { + RuntimeHints hints = new RuntimeHints(); + new MessageSourceRuntimeHints().registerHints(hints, getClass().getClassLoader()); + assertThat(RuntimeHintsPredicates.resource().forResource("messages.properties")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("messages_de.properties")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("messages_zh-CN.properties")).accepts(hints); + } + @Configuration(proxyBeanMethods = false) @PropertySource("classpath:/switch-messages.properties") static class Config {