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
This commit is contained in:
parent
8ce5fb5f06
commit
36e31c0612
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue