spring源码-国际化-3.5
一、国际化在实际代码中是非常常见的一中方式。为了结合web做一下语言上面的切换,而达到展示的目的。
二、这里呢,主要是介绍spring中对于国际化做了哪些处理。
三、实现方式
1)xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message.test</value>
</list>
</property>
</bean>
</beans>
备注:<!--这里id必须是messageSource,后续会说为什么-->
2)语言文件
写法:
testtest=\u6d4b\u8bd5{0}
testtest=test{0}
3)应用过程:
ApplicationContext context = new ClassPathXmlApplicationContext("spring-message.xml");
ResourceBundleMessageSource messageSource = context.getBean("messageSource", ResourceBundleMessageSource.class);
String messageZh = messageSource.getMessage("testtest", new Object[]{"数据"}, Locale.CHINESE);
String messageEn = messageSource.getMessage("testtest", new Object[]{"data"}, Locale.ENGLISH);
System.out.println(messageZh);
System.out.println(messageEn);
4)效果
四、当然这里只是后台的实现方式,因为是国际化吗。肯定是和web端密切相关的。传递方式
1)request:设置消息头等。通过拦截请求设置获取当前语言。然后在后端处理国际化。
2)cookie:这种方式通过请求的方式修改语言。
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge" value="1800000"/>
<property name="defaultLocale" value="zh_CN"/>
<property name="cookieName" value="Language"/>
</bean>
3)session:这个方式的好处是在于会话处理。相对于cookie,会话可以根据session的时间而保持。
<bean id="sessionLocaleResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh"/>
</bean>
五、spring部分
1)refresh()方法中的this.initMessageSource();
2)实现:
protected voidinitMessageSource() {
//获取beanfactory
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
//判断是否包含messageSource的bean
if (beanFactory.containsLocalBean("messageSource")) {
this.messageSource = (MessageSource)beanFactory.getBean("messageSource", MessageSource.class);
//如果父类存在,设置父类
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource)this.messageSource;
if (hms.getParentMessageSource() == null) {
hms.setParentMessageSource(this.getInternalParentMessageSource());
}
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Using MessageSource [" + this.messageSource + "]");
}
} else {
//spring默认的国际化使用方式
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(this.getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton("messageSource", this.messageSource);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Unable to locate MessageSource with name 'messageSource': using default [" + this.messageSource + "]");
}
}
}
3)从代码中可以看出:这里默认访问的是messageSource,如果配置成其他名称。在使用的时候会抛类型异常。
六、简易版国际化实现过程
ResourceBundle resourceBundle = ResourceBundle.getBundle("message.test", Locale.US);
String testest = resourceBundle.getString("testtest");
String format = MessageFormat.format(testest, new Object[]{"data"});
System.out.println(format);
spring源码-国际化-3.5的更多相关文章
- spring源码-bean之增强初始化-3
一.ApplicationContext的中文意思是“应用上下文”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持.资源访问(如URL和文件).事件传播 ...
- Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制
上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...
- spring源码:学习线索(li)
一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...
- spring源码浅析——IOC
=========================================== 原文链接: spring源码浅析--IOC 转载请注明出处! ======================= ...
- 【Spring源码分析】Bean加载流程概览
代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事 ...
- 【Spring源码分析】非懒加载的单例Bean初始化前后的一些操作
前言 之前两篇文章[Spring源码分析]非懒加载的单例Bean初始化过程(上篇)和[Spring源码分析]非懒加载的单例Bean初始化过程(下篇)比较详细地分析了非懒加载的单例Bean的初始化过程, ...
- 【spring源码分析】IOC容器初始化(一)
前言:spring主要就是对bean进行管理,因此IOC容器的初始化过程非常重要,搞清楚其原理不管在实际生产或面试过程中都十分的有用.在[spring源码分析]准备工作中已经搭建好spring的环境, ...
- spring源码:学习线索
一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...
- [spring源码] 小白级别的源码解析(一)
一直都在用spring,但是每次一遇到spring深入的问题,就是比较懵的状态.最近花了段时间学习了一下spring源码. 1,spring版本介绍 虽然工作中,一直在用到spring,可能有时候,并 ...
随机推荐
- 百度地图隐藏LOGO显示
在引入地图的页面加入下列样式即可隐藏百度地图左下角的LOGO <style type="text/css"> .anchorBL{display:none;} ...
- hdu-2886 Special Prime---数论推导
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2866 题目大意: 问你1到L中有多少个素数满足n^3 + p*n^2 = m^3(其中n,m为大于1 ...
- java动态代理的实现以及原理
1.前言 之前对动态代理的技术只是表面上理解,没有形成一个体系,这里总结一下,整个动态代理的实现以及实现原理,以表述的更清楚一些. 2.动态代理的实现应用到的技术 1.动态编译技术,可以使用Java自 ...
- 九.mysql数据库多实例安装mysqld_multi [start,stop,report]
经常应为系统硬件短缺,导致需要在同一台硬件服务器上面安装多个mysql实例.之前的文章四·安装mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz(基于Centos7源码 ...
- window.jQuery || document.write("<script src='__PUBLIC__/assets/js/jquery.js'>"+"<"+"/script>")
今天无意中看到这样一段代码 <script type="text/javascript"> window.jQuery || document.write(" ...
- C#一键显示及杀死占用端口号进程
private void t_btn_kill_Click(object sender, EventArgs e) { int port; bool b = int.TryParse(t_txt_gu ...
- PAT——1008. 数组元素循环右移问题
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 ...
- java中StringBuffer与String、StringBuilder的区别
在java中我们经常可以看到StringBuffer和String的用法,但是我自己在使用过程中,经常会将两者弄混淆,今天我们就来了解一下两者的区别: 我们首先来看一下我们的官方API中的简单介绍: ...
- Msql数据库连接写一个共有的连接工具
为了避免在每一个DAO中都需要自行连接connection,有多个DAO里都需要获取数据库的连接,并且在很多项目中都是一样的数据库连接. 所以就可以把获取数据库连接的代码重构到一个类里. 这样做的好处 ...
- android:windowSoftInputMode属性详解 软键盘
android:windowSoftInputMode activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性. 这个属性能影响两件事情: [一] ...