Spring - Spring容器概念及其初始化過程

引言

工做4年多,作了3年的java,每一個項目都用Spring,但對Spring一直都是知其然而不知其因此然。鄙人深知Spring是一個高深的框架,正好近期脫離加班的苦逼狀態,遂決定從Spring的官方文檔入手,結合Spring代碼和實際項目,全面的學習Spring,並將學習內容記錄在博客上,以便之後查閱並能夠和衆猿討論分享。PS:文章中會有部分是官方原句翻譯,例子也會有官方例子。

概述

Spring容器是什麼?
Spring容器是Spring的核心,一切Spring bean都存儲在Spring容器內,並由其經過IoC技術管理。Spring容器也就是一個bean工廠(BeanFactory)。應用中bean的實例化,獲取,銷燬等都是由這個bean工廠管理的。

Spring容器到底是什麼。。。
org.springframework.context.ApplicationContext接口用於完成容器的配置,初始化,管理bean。一個Spring容器就是某個實現了ApplicationContext接口的類的實例。也就是說,從代碼層面,Spring容器其實就是一個ApplicationContext。

在普通的JAVA工程中,咱們能夠經過代碼顯式new一個ClassPathXmlApplicationContext或者FileSystemXmlApplicationContext來初始化一個Spring容器。

在Web工程中,咱們通常是經過配置web.xml的方式來初始化Spring容器。

Spring配置文件

Spring配置文件以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">

  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>

  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>

  <!-- more bean definitions go here -->

</beans>

 
在eclipse中,咱們可使用SpringSource Tool Suite工具來協助咱們編寫Spring的配置文件。
工程中容許有多個Spring配置文件,咱們能夠按照本身的需求定義文件的分類,例如能夠從處理邏輯層級來區分:services.xml,daos.xml等。

另外,一個配置文件能夠引入多個其餘的配置文件,寫法以下:
<beans>

    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>

</beans>
<strong>
<span style="font-family:Microsoft YaHei;"></span></strong>
 
經過ClassPathApplicationContext初始化Spring容器

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

 
咱們寫這一句簡單的代碼,其實Spring在裏面作了不少功夫。
首先Spring會從classpath路徑下找到services.xml和daos.xml文件,
而後將文件內的配置信息讀取出來,
而後作了N多內部的初始化工做(關於容器初始化的詳細細節,後面我會補充一篇文章從源碼角度詳細說明)。

咱們還能夠用FileSystemApplicationContext來初始化Spring容器,
ApplicationContext context =
    new FileSystemXmlApplicationContext("D:/Test/services.xml");
 
這2中方式效果是同樣的,只是經過不一樣的方式讀取配置文件。
容器初始完後,咱們就能夠用這個容器來獲取咱們以前配置了個bean,簡單示例以下:
// create and configure beans
ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

// retrieve configured instance
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);

// use configured instance
List userList = service.getUsernameList();
<strong>
<span style="font-family:Microsoft YaHei;"></span></strong>
 
Web工程的Spring配置
假設咱們的web工程my-app的基礎目錄結構以下:
my-app
--src
----resources
------services.xml
--WebContent
----META-INF
----WEB-INFI
------lib
------applicationContext.xml
------web.xml
那麼咱們的web.xml須要這麼,配置方式以下
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml classpath:resources/services.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

contextConfigLocation:
contextConfigLocation指的是Spring該去哪裏讀取配置文件,ContextLoaderListener用於啓動的web容器(如tomcat)時,去讀取配置文件並完成Spring容器的初始化(包括加載bean等)。
關於contextConfigLocation的配置方式也是能夠很是豐富的,還可使用通配符 * ,這裏簡單舉幾個例子說明:
1. classpath:resources/services.xml
表示到web工程的classes/resources文件夾中查找配置文件。
2. classpath*:resources/services.xml
這種方式除了會到classes/resources文件夾查找,還會到lib下面的jar包中查找,查找路徑是jar包內/resources/services.xml。
3. classpath:resouces/**/*services.xml
這種方式表示到classpath的resources文件夾下全部文件夾(不限層級,能夠在第N層子文件夾中)中查找文件名以services.xml結尾的文件。
4. 多個路徑配置能夠用空格分開

classpath知識補充:
web工程部署後,對應war包下的WEB-INF下會有一個classes文件夾和一個lib文件,固然還有其餘的。
其中classes文件夾中的內容是從工程中的源碼文件夾(對應右鍵工程,Properties - Java Build Path - Source頁籤中看到的文件夾)中編譯過來,lib文件夾即工程中引用的jar包。
這個classes文件夾和lib中的jar都屬於classpath。

ContextLoaderListener:
這個Listener就是在標準Spring Web工程中Spring開始幹活的切入點,爲何要說標準?由於咱們能夠寫一個本身的Listener去啓動Spring容器。扯遠了~
由於ContextLoaderListener實現了ServletContextListener,因此在web容器啓動時,ContextLoaderListener就悄悄開始工做了,至於怎麼工做的還須要點篇幅去描述,這篇文件就先不細說。
相關文章
相關標籤/搜索