Springmvc国际化自动配置代码实现

所属分类: 软件编程 / java 阅读数: 33
收藏 0 赞 0 分享

在 springmvc 中通过 LocalResover 获取区域信息对象 Local(对语言信息、国家代码等的封装),进行国际化配置,在 springboot 为我们提供了默认的区域信息解析器

@Configuration(
  proxyBeanMethods = false
)
@ConditionalOnWebApplication(
  type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
  public static final String DEFAULT_PREFIX = "";
  public static final String DEFAULT_SUFFIX = "";
  private static final String[] SERVLET_LOCATIONS = new String[]{"/"};
 
    @Bean
    @ConditionalOnMissingBean  //容器中没有 LocaleResolver 时
    @ConditionalOnProperty(
      prefix = "spring.mvc",
      name = {"locale"}
    )
    public LocaleResolver localeResolver() {        //如果属性中指定用固定的就用固定的 FixedLocaleResolver 区域化信息解析器
      if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
        return new FixedLocaleResolver(this.mvcProperties.getLocale());
      } else {          //否则使用 AcceptHeaderLocaleResolver 区域化信息解析器
        AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
        localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
        return localeResolver;
      }
    }

AcceptHeaderLocaleResolver 在 request 请求头中获取到区域信息

public class AcceptHeaderLocaleResolver implements LocaleResolver {
  private final List<Locale> supportedLocales = new ArrayList(4);
 
  public Locale resolveLocale(HttpServletRequest request) {
    Locale defaultLocale = this.getDefaultLocale();
    if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
      return defaultLocale;
    } else {
      Locale requestLocale = request.getLocale();
      List<Locale> supportedLocales = this.getSupportedLocales();
      if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) {
        Locale supportedLocale = this.findSupportedLocale(request, supportedLocales);
        if (supportedLocale != null) {
          return supportedLocale;
        } else {
          return defaultLocale != null ? defaultLocale : requestLocale;
        }
      } else {
        return requestLocale;
      }
    }
  }

默认就是更具请求头带来的区域信息获取 Local 进行国际化

我们可以通过定义自己的区域化解析器生成区域化信息

public class MyLocaleResolver implements LocaleResolver {  
  @Override
  public Locale resolveLocale(HttpServletRequest request) {
    String l = request.getParameter("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) {
  }
}
 
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
  @Bean
  public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
  }
}

量变引起质变,这就是我所理解的因果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多