天天看点

Spring完整揭秘(三):Spring的IoC容器之ApplicationContext

文章目录

  • ​​ApplicationContext与BeanFactory的区别​​
  • ​​Spring统一资源加载策略​​
  • ​​ApplicationContext 与 统一资源Resource的关系​​
  • ​​ApplicationContext多配置模块加载的简化​​
  • ​​ApplicationContext与MessageSource​​

ApplicationContext与BeanFactory的区别

  • Spring提供了基本的IoC容器:​​BeanFactory​​, 在此基础上又提供了更为先进的IoC容器:ApplicationContext,该容器除了拥有BeanFactory所支持的所有功能之外,还进一步扩展了基本容器的功能:
  • 包括 BeanFactoryPostProcessor 、 BeanPostProcessor 以及其他特殊类型bean的自动识别、容器启动后bean实例的自动初始化、国际化的信息支持、容器内事件发布等
  • Spring完整揭秘(三):Spring的IoC容器之ApplicationContext

Spring统一资源加载策略

Spring完整揭秘(三):Spring的IoC容器之ApplicationContext
  • Spring框架内部使用org.springframework.core.io.Resource接口作为所有资源的抽象和访问接口:详见​​《Spring源码解析(一):认识统一资源Resource》​​。
  • 在此基础上,Spring框架通过org.springframework.core.io.ResourceLoader接口作为资源查找定位策略的统一抽象:
public interface ResourceLoader {
  /** Pseudo URL prefix for loading from the class path: "classpath:". */
  String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
  Resource getResource(String location);  
  @Nullable
  ClassLoader getClassLoader();
}      

ApplicationContext 与 统一资源Resource的关系

  • 从下图可以看出:ApplicationContext 继承了 ResourcePatternResolver ,间接实现了 ResourceLoader 接口, 即ApplicationContext 支持Spring统一资源加载策略。
  • Spring完整揭秘(三):Spring的IoC容器之ApplicationContext
  • 关系验证代码:
  • Spring完整揭秘(三):Spring的IoC容器之ApplicationContext
/**
 * ApplicationContext与Resource的关系
 *
 * @author zhuhuix
 */
public class ApplicationContext {
    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        // 通过applicationContext获取资源文件,并测试资源文件是否存在
        Resource resource1 = applicationContext.getResource("bean.xml");
        System.out.println("资源bean.xml是否存在:" + resource1.exists());
        // 通过applicationContext获取资源文件,并测试资源文件是否存在
        Resource resource2 = applicationContext.getResource("bean2.xml");
        System.out.println("资源bean2.xml是否存在:" + resource2.exists());

    }
}      
Spring完整揭秘(三):Spring的IoC容器之ApplicationContext

ApplicationContext多配置模块加载的简化

相对于 BeanFactory ,ApplicationContext 可以使用通配符进行模块加载:

  • 配置文件
  • Spring完整揭秘(三):Spring的IoC容器之ApplicationContext
<!-- bean.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"
    default-lazy-init="true">

    <description>SpringIoc</description>

    <bean id="food1" class="Food">
        <property name="foodType" value="面食"/>
        <property name="foodName" value="馄饨面"/>
        <property name="foodNum" value="1"/>
    </bean>

    <bean id="food2" class="Food">
        <property name="foodType" value="面食"/>
        <property name="foodName" value="重庆小面"/>
        <property name="foodNum" value="1"/>
    </bean>

    <bean id="person1" class="Person" scope="prototype" depends-on="food1,food2" >
        <property name="name" value="张三"/>
        <property name="foods" >
        <list>
            <ref bean="food1"/>
            <ref bean="food2"/>
        </list>
        </property>
    </bean>
</beans>

<!-- bean2.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"
    default-lazy-init="true">

    <description>SpringIoc</description>

    <bean id="food3" class="Food">
        <property name="foodType" value="炒菜"/>
        <property name="foodName" value="鱼香肉丝"/>
        <property name="foodNum" value="1"/>
    </bean>

    <bean id="food4" class="Food">
        <property name="foodType" value="炒菜"/>
        <property name="foodName" value="宫宝鸡丁"/>
        <property name="foodNum" value="1"/>
    </bean>

    <bean id="person2" class="Person" scope="prototype" depends-on="food3,food4" >
        <property name="name" value="李四"/>
        <property name="foods" >
        <list>
            <ref bean="food3"/>
            <ref bean="food4"/>
        </list>
        </property>
    </bean>
</beans>      
  • 测试代码:
/**
 * ApplicationContext Demo
 *
 * @author zhuhuix
 */
public class ApplicationContext {
    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("*.xml");
        // 通过applicationContext获取资源文件,并测试资源文件是否存在
        Resource resource1 = applicationContext.getResource("bean.xml");
        System.out.println("资源bean.xml是否存在:" + resource1.exists());

        Person person1 = (Person)applicationContext.getBean("person1");
        person1.haveLunch();

        // 通过applicationContext获取资源文件,并测试资源文件是否存在
        Resource resource2 = applicationContext.getResource("bean2.xml");
        System.out.println("资源bean2.xml是否存在:" + resource2.exists());

        Person person2 = (Person)applicationContext.getBean("person2");
        person2.haveLunch();

    }
}      
Spring完整揭秘(三):Spring的IoC容器之ApplicationContext

ApplicationContext与MessageSource

  • ApplicationContext 除了实现了 ResourceLoader 以支持统一的资源加载,它还

    实现了 MessageSource 接口,通过该接口,我们统一了国际化信息的访问方式。传入相应的 Locale 、资源的键以及相应参数,就可以取得相应的信息。

  • Spring完整揭秘(三):Spring的IoC容器之ApplicationContext
public interface MessageSource {
String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException;
String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
}      
  • ApplicationContext 容器内使用messageSource, 具备国际化功能的实际
  • Spring完整揭秘(三):Spring的IoC容器之ApplicationContext
  • 定义一个id为MessageSource的 Bean配置文件
<?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</value>
            </list>
        </property>
    </bean>
</beans>      
  • 增加两份资源文件:
  • 第一份为英语的资源文件,文件名 message_en_US.properties
Welcome=Welcome to spring
  • 第二份为简体中文的资源文件,文件名 message_zh_CN.properties
  • 测试主程序
/**
 * ApplicationContext国际化
 *
 * @author zhuhuix
 */
public class Message {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("message.xml");
        Object[] objs = new Object[]{};
        // 英文输出
        String string1 = applicationContext.getMessage("Welcome", objs, Locale.US);
        System.out.println(string1);
        // 中文输出
        String string2 = applicationContext.getMessage("Welcome", objs, Locale.CHINESE);
        System.out.println(string2);
    }
}