-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Spring MVC's locale resolver can no longer be customized in parent context #25290
Comments
@wilkinsona @bclozel I would like to discuss with you in order to see if there is way to keep achieving the goal of #25209 which is important for our GraalVM story and an interesting improvement on the JVM as well since it avoids to load On Spring Framework side, I should indeed probably remove this newly added The problem is that with current Boot autoconfiguration, Current configuration in I would need Boot to configure a default |
Yeah, I think we could do that. We document that, by default, we'll use the |
As pointed out by @jhoeller, in protected LocaleContextResolver createLocaleContextResolver() {
return new AcceptHeaderLocaleContextResolver();
}
@Bean
public LocaleContextResolver localeContextResolver() {
return createLocaleContextResolver();
} Why this out-of-the-box definition of a locale resolver is a problem with MVC but not with WebFlux? I am asking that because it would be more consistent and would avoid Spring Framework to introduce a dedicated config subclass like shown below for Spring MVC without Spring Boot purpose: @Configuration(proxyBeanMethods = false)
public class DefaultWebMvcConfiguration extends DelegatingWebMvcConfiguration {
@Bean
public LocaleResolver localeResolver() {
return new AcceptHeaderLocaleResolver();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DefaultWebMvcConfiguration.class)
public @interface EnableWebMvc {
} |
We don't have any support for customising WebFlux's If MVC switched to the WebFlux approach, users would no longer be able to provide their own |
As discussed with @wilkinsona today, it seems possible to turn those newly added beans into Spring Boot compliant ones. In a nutshell, in Boot it is possible override public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale", matchIfMissing = true)
@Override
public LocaleResolver localeResolver() {
if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
@Bean
@ConditionalOnMissingBean
@Override
public ThemeResolver themeResolver() {
return super.themeResolver();
}
@Bean
@ConditionalOnMissingBean
@Override
public FlashMapManager flashMapManager() {
return super.flashMapManager();
}
@Bean
@ConditionalOnMissingBean
@Override
public RequestToViewNameTranslator viewNameTranslator() {
return super.viewNameTranslator();
}
} The original 3 possibilities or
This also allows As a consequence I close this issue, please reopen in case of blocking point. |
I just tried to migrate a "classic" Spring MVC Webapp to 5.3.0 It has a custom
This config is no longer honored. Feels like a breaking change to me. |
I agree this is an issue, we are discussing the best solution to fix it. I will keep you informed. |
@schuch, are you using |
@schuch, would you mind creating a new issue to report this possible regression, since this issue has already been closed? |
I assumed you would be open to reopen, due to:
But yes, i can raise a new issue. |
yes, this is part of the XML configuration |
Indeed we can probably reopen it since it is not targeting a specific release yet. |
Same problem with Java Config.
Is ignored and accept-header is used. |
My mistake, after testing again it works as expect. @schuch Feel free to test on your side with the latest |
Tested successfully with |
Hi Spring Team. Just upgraded to Spring Boot 2.4.0. This is still an issue. Attaching sample project. |
@bjornharvold I think what happens is that when using For typical use case I would advise to not use |
Thank you for clarifying @sdeleuze. |
Ran into the same issue as bjornharvold however not with
|
I updated spring boot to 2.4.0. And I have the same issue with localResolver. |
I know, but if I on this option. I will disabled mechanizm which control duplicates of bean. So It open potencial debugging issue. |
@PiotrTraczynski Your repro has |
@PiotrTraczynski @schuch Complementary note: if you really need for your use case to customize Spring MVC web configuration instead of Spring Boot one, there is a third solution suggested by @rstoyanchev which is probably better than
That will give you all the flexibility to override the default beans in a Spring MVC fashion without having to set |
Thank you very much:)
pon., 16 lis 2020, 16:05 użytkownik Sébastien Deleuze <
[email protected]> napisał:
… @PiotrTraczynski <https://github.com/PiotrTraczynski> @schuch
<https://github.com/schuch> Complementary note: if you really need for
your use case to customize Spring MVC web configuration instead of Spring
Boot one, there is a third solution suggested by @rstoyanchev
<https://github.com/rstoyanchev> which is probably better than
@EnableWebMvc + spring.main.allow-bean-definition-overriding=true :
extend WebMvcConfigurationSupport and override localeResolver():
@configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport {
@bean
@OverRide
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(StringUtils.parseLocale("pl"));
return slr;
}
}
That will give you all the flexibility to override the default beans in a
Spring MVC fashion without have to set
spring.main.allow-bean-definition-overriding=true on Spring Boot side.
Ccing @snicoll <https://github.com/snicoll> in case Boot time want to
provide some guidance about that in Spring Boot 2.4 release notes.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25290 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOUPXSBLO6RZZ2QBVOYNO2TSQE5R5ANCNFSM4ODA53ZA>
.
|
I am upgrading to 5.3.
The suggestions given here and in the upgrade docs are useful but incomplete.
What worked for me was to replace
|
Another solution is to name your bean differently than "localeResolver" and annotate it with
|
Thanks for your feedback, I have updated the upgrade notes with @fdlk proposal which seems the most relevant one. |
* chore: upgrade to spring 2.4.3 * fix: move localeResolver bean definition to WebMvcConfig class See spring-projects/spring-framework#25290 (comment) * fix(web): drop special cases MethodArgumentNotValidException now extends Errors * fix: manually specify scopes Spring security behavior changed, no longer requests all scopes but only openid * fix: do not use the deprecated method * chore: upgrade to spring 2.4.10 * style: format sources
Here're some tips when using custom starter. In my projects, if a So we need to make sure
|
Hello. Faced the following problem. Overridden LocaleResolver by deriving a new class. When starting the application from the idea, everything works fine, but if you pack the application in a jar package, then when you start app the bin conflicts with what is in the class DelegatingWebMvcConfiguration. Please tell me how to solve the problem. Spring version 5.3.18 @Configuration
public class CustomWebMvcConfiguration extends DelegatingWebMvcConfiguration {
public static final String COOKIE_NAME = "lang";
public static final Locale DEFAULT_LOCALE = new Locale("ru");
public static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone(ZoneOffset.UTC);
@Bean
@NotNull
@Override
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieName(COOKIE_NAME);
resolver.setDefaultLocale(DEFAULT_LOCALE);
resolver.setDefaultTimeZone(DEFAULT_TIMEZONE);
return resolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName(COOKIE_NAME);
return interceptor;
}
} |
Faced this issue too while trying to create a bean inside a configuration imported from library using |
Without overriding beans globally (without spring.main.allow-bean-definition-overriding=true),
Use interceptor:
that will override code from DispatcherServlet, where "localeResolver" bean is set to request via request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver); |
use following is better |
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 aLocaleResolver
backs off as it is presumed that the bean that's now present is one provided by the user. This means that thespring.mvc.locale
andspring.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 byWebMvcConfigurationSupport
. This will fail as bean definition overriding is disabled by default in Boot.The text was updated successfully, but these errors were encountered: