I want to register org.springframework.web.server.i18n.LocaleContextResolver
in Spring Boot 2.3.4.RELEASE.
However, the following code not working:
Example for LocaleContextResolver defination:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.i18n.FixedLocaleContextResolver;
import org.springframework.web.server.i18n.LocaleContextResolver;
@Configuration
public class WebConfig {
@Bean
public LocaleContextResolver localeContextResolver() {
return new FixedLocaleContextResolver();
}
}
I get runtime error:
2020-09-21 09:31:47.513 ERROR 10584 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'localeContextResolver', defined in org.springframework.web.reactive.config.DelegatingWebFluxConfiguration, could not be registered. A bean with that name has already been defined in class path resource [com/github/tt4g/spring/webflux/error/handler/example/WebConfig.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
I understand that setting spring.main.allow-bean-definition-overriding=true
according to the error messages will work, but this method causes a lot of unintentional overwriting of @Bean
.
I investigated how to register a LocaleContextResolver
without setting the spring.main.allow-bean-definition-overriding=true
.
LocaleContextResolver
is provided by EnableWebFluxConfiguration
which is inner class org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration
which is implemented org.springframework.web.reactive.config.WebFluxConfigurationSupport
.
https://github.com/spring-projects/spring-framework/blob/69921b49a5836e412ffcd1ea2c7e20d41f0c0fd6/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java#L314-L324
I can override the LocaleContextResolver
by configuring a subclass of WebFluxConfigurationSupport
and adding it to @Bean
but I lost a lot of WebFlux auto-configuration.
Because WebFluxAutoConfiguration
is annotated @ConditionalOnClass(WebFluxConfigurer.class)
.
Is it possible to override the LocaleContextResolver
with WebFluxAutoConfiguration
enabled?
Comment From: tt4g
Related spring-projects/spring-framework#24693
Comment From: philwebb
I think we should align with our MVC support if possible.
Comment From: wilkinsona
I've used this issue to make the auto-configured LocaleContextResolver
back off when one's defined in an app's configuration. I've opened https://github.com/spring-projects/spring-boot/issues/23449 to add locale
and locale-resolver
properties that control the locale and how it's resolved.
Comment From: tt4g
@wilkinsona Thank you!
Comment From: wilkinsona
@tt4g No problem. I think you could work around the current limitation by changing the name of your bean to something other than localeContextResolver
and marking it @Primary
.