Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ReloadableResourceBundleMessageSource #13377

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractResourceBasedMessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -66,7 +68,9 @@ public MessageSourceProperties messageSourceProperties() {
@Bean
public MessageSource messageSource() {
MessageSourceProperties properties = messageSourceProperties();
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
AbstractResourceBasedMessageSource messageSource = (properties.isReloadable()
? new ReloadableResourceBundleMessageSource()
: new ResourceBundleMessageSource());
if (StringUtils.hasText(properties.getBasename())) {
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(properties.getBasename())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.time.temporal.ChronoUnit;

import org.springframework.boot.convert.DurationUnit;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
* Configuration properties for Message Source.
Expand Down Expand Up @@ -71,6 +73,12 @@ public class MessageSourceProperties {
*/
private boolean useCodeAsDefaultMessage = false;

/**
* Whether to use a {@link ReloadableResourceBundleMessageSource} instead of the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configuration key descriptions can't have javadoc tag (see documentation). I think we need to improve the description as high-level descriptions shouldn't contain implementation details (rather it should describe what the feature does).

* default {@link ResourceBundleMessageSource}. Recommended during development only.
*/
private boolean reloadable = false;

public String getBasename() {
return this.basename;
}
Expand Down Expand Up @@ -119,4 +127,12 @@ public void setUseCodeAsDefaultMessage(boolean useCodeAsDefaultMessage) {
this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
}

public boolean isReloadable() {
return this.reloadable;
}

public void setReloadable(boolean reloadable) {
this.reloadable = reloadable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Ignore;
import org.junit.Test;

import org.springframework.beans.BeansException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
Expand All @@ -32,6 +33,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.DelegatingMessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -220,6 +224,45 @@ public void existingMessageSourceInParentIsIgnored() {
.isEqualTo("bar")));
}

@Test
public void testDefaultReloadableValueMessageSource() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion can be added to the existing basic test

.run((context) -> {
assertThat(getDeclaredMessageSource(context))
.isInstanceOf(ResourceBundleMessageSource.class);
});
}

@Test
public void testNotReloadableMessageSource() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
"spring.messages.reloadable:false").run((context) -> {
assertThat(getDeclaredMessageSource(context))
.isInstanceOf(ResourceBundleMessageSource.class);
});
}

@Test
public void testReloadableMessageSource() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
"spring.messages.reloadable:true").run((context) -> {
assertThat(getDeclaredMessageSource(context))
.isInstanceOf(ReloadableResourceBundleMessageSource.class);
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this assertion?

.isEqualTo("bar");
});
}

private MessageSource getDeclaredMessageSource(AssertableApplicationContext context)
throws BeansException {
MessageSource messageSource = context.getBean(MessageSource.class);
if (messageSource instanceof DelegatingMessageSource) {
messageSource = ((DelegatingMessageSource) messageSource)
.getParentMessageSource();
}
return messageSource;
}

@Configuration
@PropertySource("classpath:/switch-messages.properties")
protected static class Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ content into your application. Rather, pick only the properties that you need.
spring.messages.cache-duration= # Loaded resource bundle files cache duration. When not set, bundles are cached forever. If a duration suffix is not specified, seconds will be used.
spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Whether to fall back to the system Locale if no files for a specific Locale have been found.
spring.messages.reloadable=false # Whether to use a ReloadableResourceBundleMessageSource instead of the default ResourceBundleMessageSource. Recommended during development only.
spring.messages.use-code-as-default-message=false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only.

# OUTPUT
Expand Down