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 3bd237dcd9..1d3308f2e3 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 @@ -75,6 +75,7 @@ public class MessageSourceAutoConfiguration { messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale()); messageSource.setCacheSeconds(properties.getCacheSeconds()); messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat()); + messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage()); return messageSource; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java index b4a1fd94ba..e2328d99ea 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java @@ -22,6 +22,7 @@ import java.nio.charset.Charset; * Configuration properties for Message Source. * * @author Stephane Nicoll + * @author Kedar Joshi * @since 2.0.0 */ public class MessageSourceProperties { @@ -57,6 +58,11 @@ public class MessageSourceProperties { */ private boolean alwaysUseMessageFormat = false; + /** + * Set whether to use the message code as default message instead of throwing a NoSuchMessageException. + */ + private boolean useCodeAsDefaultMessage = false; + public String getBasename() { return this.basename; } @@ -97,4 +103,11 @@ public class MessageSourceProperties { this.alwaysUseMessageFormat = alwaysUseMessageFormat; } + public boolean isUseCodeAsDefaultMessage() { + return this.useCodeAsDefaultMessage; + } + + public void setUseCodeAsDefaultMessage(final boolean useCodeAsDefaultMessage) { + this.useCodeAsDefaultMessage = useCodeAsDefaultMessage; + } } 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 19501894d5..dbe60d1e60 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 @@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer * @author EddĂș MelĂ©ndez * @author Stephane Nicoll + * @author Kedar Joshi */ public class MessageSourceAutoConfigurationTests { @@ -142,6 +143,26 @@ public class MessageSourceAutoConfigurationTests { .getPropertyValue("alwaysUseMessageFormat"); } + @Test + public void testUseCodeAsDefaultMessageDefault() throws Exception { + load("spring.messages.basename:test/messages"); + assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class))) + .isFalse(); + } + + @Test + public void testUseCodeAsDefaultMessageOn() throws Exception { + load("spring.messages.basename:test/messages", + "spring.messages.use-code-as-default-message:true"); + assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class))) + .isTrue(); + } + + private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) { + return (boolean) new DirectFieldAccessor(messageSource) + .getPropertyValue("useCodeAsDefaultMessage"); + } + @Test public void existingMessageSourceIsPreferred() { this.context = new AnnotationConfigApplicationContext();