文章目錄
- ApplicationContext與BeanFactory的差別
- Spring統一資源加載政策
- ApplicationContext 與 統一資源Resource的關系
- ApplicationContext多配置子產品加載的簡化
- ApplicationContext與MessageSource
ApplicationContext與BeanFactory的差別
- Spring提供了基本的IoC容器:BeanFactory, 在此基礎上又提供了更為先進的IoC容器:ApplicationContext,該容器除了擁有BeanFactory所支援的所有功能之外,還進一步擴充了基本容器的功能:
- 包括 BeanFactoryPostProcessor 、 BeanPostProcessor 以及其他特殊類型bean的自動識别、容器啟動後bean執行個體的自動初始化、國際化的資訊支援、容器内事件釋出等

Spring統一資源加載政策
- 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統一資源加載政策。
- 關系驗證代碼:
/**
* 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());
}
}
ApplicationContext多配置子產品加載的簡化
相對于 BeanFactory ,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();
}
}
ApplicationContext與MessageSource
-
ApplicationContext 除了實作了 ResourceLoader 以支援統一的資源加載,它還
實作了 MessageSource 接口,通過該接口,我們統一了國際化資訊的通路方式。傳入相應的 Locale 、資源的鍵以及相應參數,就可以取得相應的資訊。
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, 具備國際化功能的實際
- 定義一個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);
}
}