From 7b3bedc46880f4f1e4557d57c187c148fe04909c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 9 Jan 2018 13:18:48 +0100 Subject: [PATCH] Fix detection of dot-based resource bundle basenames This commit makes sure that properties-based resource bundle location with a dot is detected. It also harmonizes the description of the configuration key as our support is not stricly matching the convention. Closes gh-10092 --- .../context/MessageSourceAutoConfiguration.java | 12 +++++++----- .../MessageSourceAutoConfigurationTests.java | 13 +++++++++++-- .../asciidoc/appendix-application-properties.adoc | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java index 5b292d6b8f..b2b05ffa34 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,9 +59,10 @@ public class MessageSourceAutoConfiguration { private static final Resource[] NO_RESOURCES = {}; /** - * Comma-separated list of basenames, each following the ResourceBundle convention. - * Essentially a fully-qualified classpath location. If it doesn't contain a package - * qualifier (such as "org.mypackage"), it will be resolved from the classpath root. + * Comma-separated list of basenames (essentially a fully-qualified classpath + * location), each following the ResourceBundle convention with relaxed support for + * slash based locations. If it doesn't contain a package qualifier (such as + * "org.mypackage"), it will be resolved from the classpath root. */ private String basename = "messages"; @@ -180,9 +181,10 @@ public class MessageSourceAutoConfiguration { } private Resource[] getResources(ClassLoader classLoader, String name) { + String target = name.replace('.', '/'); try { return new PathMatchingResourcePatternResolver(classLoader) - .getResources("classpath*:" + name + ".properties"); + .getResources("classpath*:" + target + ".properties"); } catch (Exception ex) { return NO_RESOURCES; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java index 30a70e2750..0a8bc3d13e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,8 +60,17 @@ public class MessageSourceAutoConfigurationTests { } @Test - public void testMessageSourceCreated() throws Exception { + public void propertiesBundleWithSlashIsDetected() { load("spring.messages.basename:test/messages"); + assertThat(this.context.getBeansOfType(MessageSource.class)).hasSize(1); + assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK)) + .isEqualTo("bar"); + } + + @Test + public void propertiesBundleWithDotIsDetected() { + load("spring.messages.basename:test.messages"); + assertThat(this.context.getBeansOfType(MessageSource.class)).hasSize(1); assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK)) .isEqualTo("bar"); } diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index b213a3969c..ea49473b3b 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -114,7 +114,7 @@ content into your application; rather pick only the properties that you need. # INTERNATIONALIZATION ({sc-spring-boot-autoconfigure}/context/MessageSourceAutoConfiguration.{sc-ext}[MessageSourceAutoConfiguration]) spring.messages.always-use-message-format=false # Set whether to always apply the MessageFormat rules, parsing even messages without arguments. - spring.messages.basename=messages # Comma-separated list of basenames, each following the ResourceBundle convention. + spring.messages.basename=messages # Comma-separated list of basenames (essentially a fully-qualified classpath location), each following the ResourceBundle convention with relaxed support for slash based locations. spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever. spring.messages.encoding=UTF-8 # Message bundles encoding. spring.messages.fallback-to-system-locale=true # Set whether to fall back to the system Locale if no files for a specific Locale have been found.