Spring学习篇:IoC知识整理(二)

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。如下代码:

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>format</value>
                <value>window</value>
            </list>
        </property>
    </bean>

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.properties
message=你好

# format_en_US.properties
message=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 world
The 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众多注意事项和特性有深入详解,用到时可以随时查阅。

原文地址:https://www.cnblogs.com/xiaoluo501395377/p/2732016.html