Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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;
Expand All @@ -26,13 +28,15 @@
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;
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;
Expand All @@ -48,13 +52,15 @@
* @author Dave Syer
* @author Phillip Webb
* @author Eddú Meléndez
* @author Marc Becker
* @since 1.5.0
*/
@AutoConfiguration
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
@ImportRuntimeHints(MessageSourceRuntimeHints.class)
public class MessageSourceAutoConfiguration {

private static final Resource[] NO_RESOURCES = {};
Expand Down Expand Up @@ -125,4 +131,13 @@ private Resource[] getResources(ClassLoader classLoader, String name) {

}

static class MessageSourceRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.resources().registerPattern("messages.properties").registerPattern("messages_*.properties");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
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;
Expand All @@ -40,6 +43,7 @@
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Kedar Joshi
* @author Marc Becker
*/
class MessageSourceAutoConfigurationTests {

Expand Down Expand Up @@ -180,6 +184,15 @@ void messageSourceWithNonStandardBeanNameIsIgnored() {
.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 {
Expand Down