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
- fix: move localeResolver bean definition to WebMvcConfig class See https://github.com/spring-projects/spring-framework/issues/25290#issuecomment-802218753 — committed to fdlk/molgenis by fdlk 6 years ago
- fix: move localeResolver bean definition to WebMvcConfig class See https://github.com/spring-projects/spring-framework/issues/25290#issuecomment-802218753 — committed to fdlk/molgenis by fdlk 6 years ago
- EnhancedLocaleResolver bean declaration moved to APIConfig.java to fix https://github.com/spring-projects/spring-framework/issues/25290 — committed to tesler-platform/tesler by deleted user 5 years ago
- fix: move localeResolver bean definition to WebMvcConfig class See https://github.com/spring-projects/spring-framework/issues/25290#issuecomment-802218753 — committed to fdlk/molgenis by fdlk 6 years ago
- fix: move localeResolver bean definition to WebMvcConfig class See https://github.com/spring-projects/spring-framework/issues/25290#issuecomment-802218753 — committed to fdlk/molgenis by fdlk 6 years ago
- chore: upgrade to spring 2.4.10 (#9188) * chore: upgrade to spring 2.4.3 * fix: move localeResolver bean definition to WebMvcConfig class See https://github.com/spring-projects/spring-framework... — committed to molgenis/molgenis by fdlk 5 years ago
I am upgrading to 5.3.
WebMvcConfigurerimplementationsThe suggestions given here and in the upgrade docs are useful but incomplete.
WebMvcConfigurer, you getBeanDefinitionOverrideException, because it clashes with the bean created in theDelegatingWebMvcConfiguration.@EnableWebMvcwith aWebMvcConfigthat extends WebMvcConfigurationSupport, the application boots but the WebMvcConfigurers get skipped(!)What worked for me was to replace
@EnableWebMvcwith aWebMvcConfigthat extendsDelegatingWebMvcConfigurationinstead ofWebMvcConfigurationSupport: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
@EnableWebMvcSpring Boot web config backs off, so@ConditionalOnMissingBeanonLocalResolverbean which is Boot specific is not taken anymore in account and you are just using Spring MVCLocaleResolverbean which requiresspring.main.allow-bean-definition-overriding=trueas rightfully explained in the error message. I think this is the expected behavior.For typical use case I would advise to not use
@EnableWebMvcwith Boot, and if you really need it,settingspring.main.allow-bean-definition-overriding=trueis the way to go.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" />toservlet-context.xml. With logging configured withlog4j.category.org.springframework.web.servlet.DispatcherServlet=DEBUGI can see the bean customized because without the bean declaration I see:And with it:
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
@Beanconfiguration in the default infrastructure Spring provides (instead of the defaultDispatcherServlet.properties).With 5.3, a
LocaleResolverbean in an arbitrary@Configuationwill not be used by Spring MVC anymore.But it will be taken in account if you add it as part of the Spring MVC configuration via
WebMvcConfigurationSupportclass:Or Spring MVC
WebMvcConfigurer: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
@ConditionalOnMissingBeanoverride.I just tried to migrate a “classic” Spring MVC Webapp to 5.3.0
It has a custom
LocaleResolverdefined inapplicationContext.xmlThis 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
yes, this is part of the XML configuration