Make the auto-configured LocaleContextResolver conditional on missing bean

Closes gh-23419
pull/23456/head
Andy Wilkinson 4 years ago
parent 20193715f9
commit 659b459fab

@ -69,6 +69,7 @@ import org.springframework.web.reactive.result.method.annotation.ArgumentResolve
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.i18n.LocaleContextResolver;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link EnableWebFlux WebFlux}. * {@link EnableAutoConfiguration Auto-configuration} for {@link EnableWebFlux WebFlux}.
@ -264,6 +265,13 @@ public class WebFluxAutoConfiguration {
return super.createRequestMappingHandlerMapping(); return super.createRequestMappingHandlerMapping();
} }
@Bean
@Override
@ConditionalOnMissingBean
public LocaleContextResolver localeContextResolver() {
return super.localeContextResolver();
}
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

@ -73,6 +73,8 @@ import org.springframework.web.reactive.result.method.annotation.RequestMappingH
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.reactive.result.view.ViewResolutionResultHandler; import org.springframework.web.reactive.result.view.ViewResolutionResultHandler;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
import org.springframework.web.server.i18n.LocaleContextResolver;
import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPattern;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -452,6 +454,13 @@ class WebFluxAutoConfigurationTests {
}); });
} }
@Test
void customLocaleContextResolver() {
this.contextRunner.withUserConfiguration(LocaleContextResolverConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(LocaleContextResolver.class)
.hasBean("customLocaleContextResolver"));
}
private Map<PathPattern, Object> getHandlerMap(ApplicationContext context) { private Map<PathPattern, Object> getHandlerMap(ApplicationContext context) {
HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class); HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class);
if (mapping instanceof SimpleUrlHandlerMapping) { if (mapping instanceof SimpleUrlHandlerMapping) {
@ -676,4 +685,14 @@ class WebFluxAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class LocaleContextResolverConfiguration {
@Bean
LocaleContextResolver customLocaleContextResolver() {
return new AcceptHeaderLocaleContextResolver();
}
}
} }

Loading…
Cancel
Save