springboot自定义localeResolver不生效问题

在项目国际化过程中,自定义了localeResolver,但是不生效,页面传参无问题。
通过分析WebMvcAutoConfiguration的自动配置信息

@Bean
		@ConditionalOnMissingBean
		@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
		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;
		}

@ConditonalOnMissingBean是当容器中没有该bean时,springboot自动配置,判断bean的方式是bean的id,我们配置Bean的方法就是Bean的id,查看配置类中配置localeResolver的方法,发现方法名不是localeResolver,因此springboot的配置仍然生效

   @Bean
    public LocaleResolver LocaleResolver(){
        return new MyLocaleResolver();
    }

LocaleResolver的“L”为大写…找半天错误,好细
改变方法名为localResolver即可

   @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

你可能感兴趣的:(spring,boot)