前言:Spring有两个核心接口(BeanFactory和ApplicationContext),其中ApplicationContext是BeanFactory的子接口,它们都可以代表Spring容器,而Spring容器就是生成Bean实例的工厂,并管理容器中的Bean。而ApplicationContext作为功能更强大的Spring容器,提供了诸如资源访问(URL和文件)、事件机制、同时加载多个配置文件、国际化支持等扩展功能。因此,本文介绍在项目中常用到的功能模块
本篇文章重点关注以下问题:
- ApplicationContext的国际化支持
- ApplicationContext的事件机制
- 在Bean中获取Spring容器
1. ApplicationContext的国际化支持
ApplicationContext接口继承了MessageSource接口,因此具备了国际化功能。下面是MessageSource接口中定义的三个用于国际化的方法:
public interface MessageSource { /** * 尝试解析国际化信息,如果为找到相关国际化配置,则返回默认信息 * @param code 待国际化信息在配置文件中的关键字 * @param args 待国际化信息中占位符的值 * @param defaultMessage 查找国家化配置失败后的默认返回值 * @param locale Locale信息 * @return */ String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale); /** * 尝试解析国际化信息,如果为找到相关国际化配置,则返回null * @param code 待国际化信息在配置文件中的关键字 * @param args 待国际化信息中占位符的值 * @param locale Locale信息 * @return */ String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException; /** * 以国际化解析器进行国际化 * @param resolvable 国际化解析接口 * @param locale Locale信息 * @return */ String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException; }
1.1 在Spring中配置MessageSource的Bean:通常使用ResourceBundleMessageSource类
<?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-4.3.xsd"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <!-- 驱动Spring调用messageSource Bean的setBasenames()方法,该方法需要一个数组参数,使用list元素配置多个数组元素 --> <property name="basenames"> <list> <value>com/wj/chapter2/applicationContextg/i18/message</value> <!-- 如果有多个资源文件,全部列在此处 --> </list> </property> </bean> </beans>上面配置文件中的粗体字只指定了一份国际化资源文件,其baseName是message,然后给出它的两份资源文件:message_en_US.properties、message_zh_CN.properties。
首先是美式资源文件:message_en_US.properties
hello=welcome,{0} now=now is :{0}然后是中式资源文件:message_zh_CN.properties。
hello=欢迎你,{0} now=现在时间是 :{0}
因为资源文件中含有简体中文,所以需要用native2ascii工具将这份资源文件国际化(native2ascii 源文件 目标文件),得到的文件内容如下:
hello=\u6b22\u8fce\u4f60\uff0c{0} now=\u73b0\u5728\u65f6\u95f4\u662f\uff1a{0}
至此,程序就拥有了两份资源文件,可用于自适应美式英语和简体中文环境。
1.3 编写国际化功能测试代码
public class I18Main { // 1.指明xml配置文件位置,便于Spring读取,从而知道Bean的相关信息 private static final String PATH_XML = "com/wj/chapter2/applicationContextg/i18/applicationContext.xml"; @SuppressWarnings("resource") public static void main(String[] args)throws Exception { // 2.根据xml配置文件,创建Spring IOC容器的上下文 ApplicationContext ctx = new ClassPathXmlApplicationContext(PATH_XML); // 3.使用getMessage()方法获取本地化消息。(Locale的getDefault方法返回计算机环境的默认Locale) String hello = ctx.getMessage("hello" , new String[]{"熊燕子"}, Locale.getDefault(Locale.Category.FORMAT)); String now = ctx.getMessage("now" , new Object[]{new Date()}, Locale.getDefault(Locale.Category.FORMAT)); // 打印出两条本地化消息 System.out.println(hello); System.out.println(now); } }
1.4 查看运行结果
2. ApplicationContext的事件机制
ApplicationContext的事件机制是典型的观察者设计模式,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext的事件处理。如果容器中有一个ApplicationListener的Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener的Bean都将自动触发。
Spring的事件框架有如下两个重要成员:
- ApplicationEvent:容器事件,必须由ApplicationContext发布。
- ApplicationListener:监听器,可由容器中的任何监听器Bean担任。(必须实现ApplicationListener接口)
2.1 首先定义一个Spring容器事件
/** * 定义容器事件,此事件必须由容器ApplicationContext发布 * @author Administrator */ public class EmailEvent extends ApplicationEvent { private static final long serialVersionUID = 1259505481443188920L; private String address; private String text; public EmailEvent(Object source) { super(source); } // 初始化全部成员变量的构造器 public EmailEvent(Object source , String address , String text) { super(source); this.address = address; this.text = text; } // setter|getter public String getAddress() { return address; } public String getText() { return text; } public void setAddress(String address) { this.address = address; } public void setText(String text) { this.text = text; } }上面的EmailEvent类继承了ApplicationEvent,则该对象就可作为Spring容器的容器事件,便可通过容器发布。 2.2 实现容器事件的监听类