Skip to content

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

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

Closed
tt4g opened this issue Sep 21, 2020 · 5 comments
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@tt4g
Copy link

tt4g commented Sep 21, 2020

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?

@tt4g
Copy link
Author

tt4g commented Sep 21, 2020

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 21, 2020
@philwebb
Copy link
Member

philwebb commented Sep 21, 2020

I think we should align with our MVC support if possible.

@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 21, 2020
@philwebb philwebb added this to the 2.4.x milestone Sep 21, 2020
@wilkinsona wilkinsona changed the title Can not register custom LocaleContextResolver with WebFluxAutoConfiguration enabled Allow users to define their own LocaleContextResolver bean when using WebFlux Sep 22, 2020
@wilkinsona wilkinsona self-assigned this Sep 22, 2020
@wilkinsona wilkinsona modified the milestones: 2.4.x, 2.4.0-RC1 Sep 22, 2020
@wilkinsona
Copy link
Member

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 #23449 to add locale and locale-resolver properties that control the locale and how it's resolved.

@tt4g
Copy link
Author

tt4g commented Sep 22, 2020

@wilkinsona Thank you!

@wilkinsona
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants