自定义localeResolver不生效问题

问题:

在国际化过程中,自定义了localeResolver并且@Bean注入,但不生效

我原本注入

public class MyLocalResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        System.out.println(l);
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
    @Bean
    public LocaleResolver mylocaleResolver(){
        return new MyLocalResolver();
    }

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

解决方法

将方法名改为: localeResolver

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

你可能感兴趣的:(自定义localeResolver不生效问题)