spring-framework: Spring MVC's locale resolver can no longer be customized in parent context

Affects: 5.3 snapshots

The fix for #25209 appears to have removed or at least limited the ability to customize the locale resolver that is used by the DispatcherServlet.

The introduction of a default LocaleResolver bean means that Spring Boot’s auto-configuration of a LocaleResolver backs off as it is presumed that the bean that’s now present is one provided by the user. This means that the spring.mvc.locale and spring.mvc.locale-resolver properties are no longer honoured.

This change will also break any Spring Boot user who has defined their own localeResolver bean as it would attempt to override the bean defined by WebMvcConfigurationSupport. This will fail as bean definition overriding is disabled by default in Boot.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 43 (20 by maintainers)

Commits related to this issue

Most upvoted comments

I am upgrading to 5.3.

  • it’s a WebMVC application, no Spring Boot
  • with a custom LocaleResolver bean
  • and multiple WebMvcConfigurer implementations

The suggestions given here and in the upgrade docs are useful but incomplete.

  • If you move the localeResolver bean factory method to a WebMvcConfigurer, you get BeanDefinitionOverrideException, because it clashes with the bean created in the DelegatingWebMvcConfiguration.
  • If you replace @EnableWebMvc with a WebMvcConfig that extends WebMvcConfigurationSupport, the application boots but the WebMvcConfigurers get skipped(!)

What worked for me was to replace @EnableWebMvc with a WebMvcConfig that extends DelegatingWebMvcConfiguration instead of WebMvcConfigurationSupport:

@Configuration
public class WebMvcConfig extends DelegatingWebMvcConfiguration {
  @Bean
  @Override
  public LocaleResolver localeResolver() { ... }
}

Thanks for your feedback, I have updated the upgrade notes with @fdlk proposal which seems the most relevant one.

@bjornharvold I think what happens is that when using @EnableWebMvc Spring Boot web config backs off, so @ConditionalOnMissingBean on LocalResolver bean which is Boot specific is not taken anymore in account and you are just using Spring MVC LocaleResolver bean which requires spring.main.allow-bean-definition-overriding=true as rightfully explained in the error message. I think this is the expected behavior.

For typical use case I would advise to not use @EnableWebMvc with Boot, and if you really need it,setting spring.main.allow-bean-definition-overriding=true is the way to go.

@schuch Feel free to test on your side with the latest 5.3.1-SNAPSHOT.

Tested successfully with 5.3.1-SNAPSHOT 🥳

Solution at: https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x#spring-mvc do not work. there is still override error. How to customize LocalResolver?

I will try to reproduce this on greenfield and provide a small repo this week.

@schuch I have created a sample web XML project using spring-framework-issues scripts and I can’t reproduce the issue. What I did is adding <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver" /> to servlet-context.xml. With logging configured with log4j.category.org.springframework.web.servlet.DispatcherServlet=DEBUG I can see the bean customized because without the bean declaration I see:

DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Detected AcceptHeaderLocaleResolver>

And with it:

DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Detected FixedLocaleResolver>

Could you please provide a repro project?

@nikomiranda On Javaconfig side for non Boot applications there is an expected change of behavior that I will shortly document in the upgrade notes and will also explain that more in detail in the reference documentation.

We have done those changes in order to improve GraalVM compatibility (avoid reflection usage in the default code path) and also to unify the web configuration around @Bean configuration in the default infrastructure Spring provides (instead of the default DispatcherServlet.properties).

With 5.3, a LocaleResolver bean in an arbitrary @Configuation will not be used by Spring MVC anymore.

@Configuration
public class FooConfig {
	@Bean
	public LocaleResolver localeResolver() {
		return new FixedLocaleResolver(new Locale("fr", "FR"));
	}
}

But it will be taken in account if you add it as part of the Spring MVC configuration via WebMvcConfigurationSupport class:

@EnableWebMvc
@ComponentScan(basePackages="org.springframework.issues")
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
	@Bean
	@Override
	public LocaleResolver localeResolver() {
		return new FixedLocaleResolver(new Locale("fr", "FR"));
	}
}

Or Spring MVC WebMvcConfigurer:

@EnableWebMvc
@ComponentScan(basePackages="org.springframework.issues")
@Configuration
public class WebConfig implements WebMvcConfigurer {
	@Bean
	public LocaleResolver localeResolver() {
		return new FixedLocaleResolver(new Locale("fr", "FR"));
	}
}

You have of course also the option to provide your own Spring MVC configuration.

For Boot application the behavior will be unchanged thanks to the @ConditionalOnMissingBean override.

I just tried to migrate a “classic” Spring MVC Webapp to 5.3.0

It has a custom LocaleResolver defined in applicationContext.xml

<bean id="localeResolver" class="...CustomLocaleResolver" />

This config is no longer honored. Feels like a breaking change to me.

Thank you for clarifying @sdeleuze.

@sdeleuze Yes. That worked. It need to be documented on the upgrade notes!

Thank you

@schuch, are you using <mvc:annotation-driven /> in your XML config file?

yes, this is part of the XML configuration