Skip to content

Commit c236db0

Browse files
committed
Ignore parent contexts in message source auto-configuration
This commit applies the changes made in 68b55ad to 1.2.x (it was originally only made in 1.0.x and master). It also adds some tests. Closes gh-3803
1 parent 35a3f4a commit c236db0

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
2424
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
2627
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
2728
import org.springframework.boot.context.properties.ConfigurationProperties;
2829
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -50,7 +51,7 @@
5051
* @author Phillip Webb
5152
*/
5253
@Configuration
53-
@ConditionalOnMissingBean(MessageSource.class)
54+
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
5455
@Order(Ordered.HIGHEST_PRECEDENCE)
5556
@Conditional(ResourceBundleCondition.class)
5657
@EnableConfigurationProperties

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
import org.junit.Ignore;
2222
import org.junit.Test;
2323
import org.springframework.boot.test.EnvironmentTestUtils;
24+
import org.springframework.context.ConfigurableApplicationContext;
25+
import org.springframework.context.MessageSource;
26+
import org.springframework.context.MessageSourceResolvable;
27+
import org.springframework.context.NoSuchMessageException;
2428
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29+
import org.springframework.context.annotation.Bean;
2530
import org.springframework.context.annotation.Configuration;
2631
import org.springframework.context.annotation.PropertySource;
2732

@@ -107,9 +112,69 @@ public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
107112
this.context.getMessage("foo", null, "Foo message", Locale.UK));
108113
}
109114

115+
@Test
116+
public void existingMessageSourceIsPreferred() {
117+
this.context = new AnnotationConfigApplicationContext();
118+
this.context.register(CustomMessageSource.class,
119+
MessageSourceAutoConfiguration.class,
120+
PropertyPlaceholderAutoConfiguration.class);
121+
this.context.refresh();
122+
assertEquals("foo", this.context.getMessage("foo", null, null, null));
123+
}
124+
125+
@Test
126+
public void existingMessageSourceInParentIsIgnored() {
127+
ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext();
128+
parent.refresh();
129+
try {
130+
this.context = new AnnotationConfigApplicationContext();
131+
this.context.setParent(parent);
132+
EnvironmentTestUtils.addEnvironment(this.context,
133+
"spring.messages.basename:test/messages");
134+
this.context.register(MessageSourceAutoConfiguration.class,
135+
PropertyPlaceholderAutoConfiguration.class);
136+
this.context.refresh();
137+
assertEquals("bar",
138+
this.context.getMessage("foo", null, "Foo message", Locale.UK));
139+
}
140+
finally {
141+
parent.close();
142+
}
143+
}
144+
110145
@Configuration
111146
@PropertySource("classpath:/switch-messages.properties")
112147
protected static class Config {
113148

114149
}
150+
151+
@Configuration
152+
protected static class CustomMessageSource {
153+
154+
@Bean
155+
public MessageSource messageSource() {
156+
return new MessageSource() {
157+
158+
@Override
159+
public String getMessage(String code, Object[] args,
160+
String defaultMessage, Locale locale) {
161+
return code;
162+
}
163+
164+
@Override
165+
public String getMessage(String code, Object[] args, Locale locale)
166+
throws NoSuchMessageException {
167+
return code;
168+
}
169+
170+
@Override
171+
public String getMessage(MessageSourceResolvable resolvable,
172+
Locale locale) throws NoSuchMessageException {
173+
return resolvable.getCodes()[0];
174+
}
175+
176+
};
177+
}
178+
179+
}
115180
}

0 commit comments

Comments
 (0)