Skip to content

Allow users to define their own LocaleContextResolver bean when using WebFlux #23419

@tt4g

Description

@tt4g

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?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions