Spring-国际化信息03-容器级的国际化信息资源
2017-08-12 13:07
525 查看
导读
概述
实例
注意事项
Spring-国际化信息02-MessageSource接口
Spring-国际化信息03-容器级的国际化信息资源
ApplicationContext 实现了 MessageSource 接口。
在一般情况下,国际化信息资源应该是容器级。我们一般不会将MessageSource作为一个Bean注入到其他的Bean中,相反MessageSource作为容器的基础设施向容器中所有的Bean开放。
国际化信息一般在系统输出信息时使用,如Spring MVC的页面标签,控制器Controller等,不同的模块都可能通过这些组件访问国际化信息,因此Spring就将国际化消息作为容器的公共基础设施对所有组件开放。
Spring根据反射机制从BeanDefinitionRegistry中找出名称为“messageSource”且类型为org.springframework.context.MessageSource的Bean,将这个Bean定义的信息资源加载为容器级的国际化信息资源.
资源文件
配置文件
测试类
运行结果:
假设我们将id=”messageSource” 改为 id=”messageSource1”
再此运行
概述
实例
注意事项
导读
Spring-国际化信息01-基础知识Spring-国际化信息02-MessageSource接口
Spring-国际化信息03-容器级的国际化信息资源
概述
我们查看ApplicationContext中的源码可以看到public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver
ApplicationContext 实现了 MessageSource 接口。
在一般情况下,国际化信息资源应该是容器级。我们一般不会将MessageSource作为一个Bean注入到其他的Bean中,相反MessageSource作为容器的基础设施向容器中所有的Bean开放。
国际化信息一般在系统输出信息时使用,如Spring MVC的页面标签,控制器Controller等,不同的模块都可能通过这些组件访问国际化信息,因此Spring就将国际化消息作为容器的公共基础设施对所有组件开放。
Spring根据反射机制从BeanDefinitionRegistry中找出名称为“messageSource”且类型为org.springframework.context.MessageSource的Bean,将这个Bean定义的信息资源加载为容器级的国际化信息资源.
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster资源文件
greeting.common=How are you {0}?,today is {1} greeting.morning=Good Morning {0}! now is {1,time,short} greeting.afternoon=Good Afternoon {0}! now is {1,date,long}
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--①注册资源Bean,其Bean名称只能为messageSource --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames" ref="resourceList"/> </bean> <util:list id="resourceList"> <value>i18n/fmt_resource</value> </util:list> </beans>
测试类
package com.xgj.ioc.i18n.container; import java.util.GregorianCalendar; import java.util.Locale; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ContainerI18NTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/ioc/i18n/container/beans.xml"); // 动态参数 Object[] params = { "XiaoGongJiang", new GregorianCalendar().getTime() }; // 直接通过容器访问国际化信息 String msg1 = ctx.getMessage("greeting.common", params, Locale.US); String msg2 = ctx.getMessage("greeting.morning", params, Locale.CHINA); System.out.println(msg1); System.out.println(msg2); } }
运行结果:
注意事项
MessageSource Bean名字必须命名为“messageSource”,以上代码将抛出NoSuchMessageException异常假设我们将id=”messageSource” 改为 id=”messageSource1”
再此运行
相关文章推荐
- Spring中的国际化资源以及视图跳转
- Spring boot 国际化自动加载资源文件去除默认国际化文件
- SpringMVC之国际化资源显示错误信息
- Spring学习总结4(消息资源国际化)
- spring配置国际化的消息资源文件以及spring3.0注解表单验证
- (spring-第14回【IoC基础篇】)国际化信息 (转)
- struts2-19配置国际化全局资源文件、输出国际化信息
- struts、spring国际化资源文件使用简介
- spring和java国际化资源文件加载顺序
- 复习struts2之配置国际化全局资源文件、输出国际化信息
- 学习Spring必学的Java基础知识(8)----国际化信息
- 使用Spring中Src下 -context.xml : 用资源文件夹保存数据库的链接信息 并在配置文件里读取
- 配置国际化全局资源文件、输出国际化信息
- Spring笔记之八(国际化,资源文件的加载)
- SpringBoot学习之验证信息国际化
- Struts2_国际化、配置全局资源与输入国际化信息
- Struts2学习笔记(十六):配置国际化全局资源文件、输出国际化信息
- Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验 使用消息资源文件对消息国际化
- Spring的国际化资源messageSource
- Struts2的国际化、配置全局资源与输出国际化信息、国际化—JSP中直接访问某个资源文件