博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring学习篇:IoC知识整理(二)
阅读量:7126 次
发布时间:2019-06-28

本文共 2863 字,大约阅读时间需要 9 分钟。

1.BeanFactory还是AppliactionContext:

这个我想大家肯定会选择ApplicationContext,这也是Spring推荐选用的,ApplicationContext本身拓展了BeanFactory接口,而且对于一些特性,BeanFactory配置好了却无法实现,例如事务管理和AOP。我们来看下Spring官方文档提供的BeanFactory和ApplicationContext两个接口所提供的功能:

从这我们基本可以看出ApplicationContext的优势所在,所以开发中强烈建议使用ApplicationContext。

2.利用MessageSource实现国际化(i18N):

ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化)。MessageSource接口定义了如下几个方法:

  • String getMessage(String code, Object[] args, String default, Locale locale):用来从MessageSource获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的MessageFormat来作消息中替换值。
  • String getMessage(String code, Object[] args, Locale loc):本质上和上一个方法相同,其区别在:没有指定默认值,如果没找到消息,会抛出一个NoSuchMessageException异常。

  • String getMessage(MessageSourceResolvable resolvable, Locale locale):上面方法中所使用的属性都封装到一个MessageSourceResolvable实现中,而本方法可以指定MessageSourceResolvable实现。

当一个ApplicationContext被加载时,它会自动在context中查找已定义为MessageSource类型的bean。此bean的名称须为messageSource。如果找到,那么所有对上述方法的调用将被委托给该bean。否则ApplicationContext会在其父类中查找是否含有同名的bean。如果有,就把它作为MessageSource。如果它最终没有找到任何的消息源,一个空的StaticMessageSource将会被实例化,使它能够接受上述方法的调用。

Spring目前提供了两个MessageSource的实现:ResourceBundleMessageSourceStaticMessageSource。它们都继承NestingMessageSource以便能够处理嵌套的消息。StaticMessageSource很少被使用,但能以编程的方式向消息源添加消息。我们基本上都是用ResourceBundleMessageSource来实例化bean。如下代码:

format
window

id属性命名为messageSource,class为ResourceBundleMessageSource,我们在'applicationContext.xml'的文件中(在classpath根目录下)定义了一个messageSource bean,通过它的basenames属性引用多个资源文件;而basenames属性值由list元素所指定的三个值传入,它们以文件的形式存在并被放置在classpath的根目录下(分别为format_zh_CN.propertiesformat_en_US.propertieswindows.properties)(注:我这里使用到了国际化,所以format定义了两个properties,在测试类中通过指定Locale设置的属性自动去寻找对应的国际化资源)。

三个属性文件(format_zh_CN.propertiesformat_en_US.propertieswindows.properties)代码如下:

# format_zh_CN.propertiesmessage=你好# format_en_US.propertiesmessage=hello world# window.properties//这里使用了两个占位符{0}和{1},在使用时通过object[]传值进去message2=The arguments {0} {1} is required\!

测试类代码如下:

MessageSource source = new FileSystemXmlApplicationContext("src/applicationContext.xml");                String message = source.getMessage("message", null, Locale.US);                System.out.println(message);             //Object[]数组里面可以存放若干个元素,属性文件中有几个占位符,就按顺序自动为占位符附上值        String message2 = source.getMessage("message2", new Object[]{"dataSource", "default"}, "default", null);                System.out.println(message2);

console输出结果如下:

hello worldThe arguments dataSource default is required!

因为这里使用了国际化,如果我将String message = source.getMessage("message", null, Locale.US);改为String message = source.getMessage("message", null, Locale.CHINA);时,console输出结果如下:

 

你好The arguments dataSource default is required!

 

IoC我觉得要注意的是这些,其实Spring官方文档对IoC众多注意事项和特性有深入详解,用到时可以随时查阅。

 

转载地址:http://jchel.baihongyu.com/

你可能感兴趣的文章
数据中心那些常见的问题
查看>>
Linux服务器关闭/开启ICMP协议(ping)
查看>>
我的友情链接
查看>>
Linux基础
查看>>
Linux逻辑卷快照及ssm的使用
查看>>
关于工资的三个秘密
查看>>
NFS服务器配置
查看>>
APUE读书笔记-05标准输入输出库(2)
查看>>
mysql中replace into、replace into、insert ignore用法区别
查看>>
Sql Server系列:数据库组成及系统数据库
查看>>
***之open***
查看>>
tengine+uwsgi+django
查看>>
Django 模板语法取值
查看>>
oracle和sql server 比较
查看>>
高端存储亟待国产化
查看>>
DHCP分配租期问题的重要性
查看>>
linux负载均衡
查看>>
NATIVE SQL的用法
查看>>
JS多项选择删除
查看>>
scrollLeft、offsetLeft、clientLeft、clientHeight详解
查看>>