如下內容爲我的理解,若有誤還請留言指出,不勝感激!html
Web容器java
web容器(web服務器)主要有:Apache、IIS、Tomcat、Jetty、JBoss、webLogic等,而Tomcat、Jetty、JBoss、webLogic同時也是servlet容器,或者說他們還包含了servlet容器。沒有servlet容器,你也能夠用web容器直接訪問靜態頁面,好比安裝一個apache等,可是若是要顯示jsp/servlet,你就要安裝一個servlet容器了,可是光有servlet容器是不夠的,由於它要被解析成html輸出,因此你仍須要一個web容器。大多數servlet容器同時提供了web容器的功能,也就是說大多servelt容器能夠獨立運行你的web應用。web
web容器是管理servlet(經過servlet容器),以及監聽器(Listener)和過濾器(Filter)的。這些都是在web容器的掌控範圍裏。但他們不在spring和springmvc的掌控範圍裏。所以,咱們沒法在這些類中直接使用Spring註解的方式來注入咱們須要的對象,是無效的,web容器是沒法識別的。spring
但咱們有時候又確實會有這樣的需求,好比在容器啓動的時候,作一些驗證或者初始化操做,這時可能會在監聽器裏用到bean對象;又或者須要定義一個過濾器作一些攔截操做,也可能會用到bean對象。
那麼在這些地方怎麼獲取spring的bean對象呢?下面我提供兩個方法:apache
1、tomcat
public void contextInitialized(ServletContextEvent sce) { ApplicationContext context = (ApplicationContext) sce.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); UserService userService = (UserService) context.getBean("userService"); }
2、安全
public void contextInitialized(ServletContextEvent sce) { WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); UserService userService = (UserService) webApplicationContext.getBean("userService"); }
注意:以上代碼有一個前提,那就是servlet容器在實例化ConfigListener並調用其方法以前,要確保spring容器已經初始化完畢!而spring容器的初始化也是由Listener(ContextLoaderListener)完成,所以只需在web.xml中先配置初始化spring容器的Listener,而後在配置本身的Listener。服務器
TOMCAT多線程
在Tomcat中有4種級別的容器:Engine,Host,Context和Wrapper。mvc
Engine:整個Catalina Servlet引擎;
Host:包含一個或多個Context容器的虛擬主機;
Context:表示一個Web應用程序,對應着一個Servlet上下文(ServletContext),能夠包含多個Wrapper;
Wrapper:表示一個獨立的Servlet;
4個層級接口的標準實現分別是:StandardEngine類,StandardHost類,StandardContext類和StandardWrapper類。它們在org.apache.catalina.core包下。
Tomcat結構目錄
/bin:包含啓動和關閉Tomcat的文件
/conf:包含不一樣的配置文件:server.xml,web.xml,tomcat-user.xml
/lib:包含Tomcat使用的JAR文件
/logs:包含日誌文件
/webapps:包含應用程序示例及本身開發的程序
/work:包含有JSP生成的Servlet
Servlet容器
Servlet容器是管理servlet對象的。
Servlet容器的做用:
負責處理客戶請求,當客戶請求來到時,Servlet容器獲取請求,而後調用某個Servlet,並把Servlet的執行結果返回給客戶。
使用Servlet容器的緣由:
通訊支持:利用容器提供的方法,你能輕鬆的讓servlet與web服務器對話,而不用本身創建serversocket、監聽某個端口、建立流等 等。容器知道本身與web服務器之間的協議,因此你的servlet不用擔憂web服務器(如Apache)和你本身的web代碼之間的API,只須要考慮如何在servlet中實現業務邏輯(如處理一個訂單)。
生命週期管理:servlet容器控制着servlet的生與死,它負責加載類、實例化和初始化
servlet,調用servlet方法,以及使servlet實例被垃圾回收,有了servlet容器,你不須要太多的考慮資源管理。
多線程支持:容器會自動爲它所接收的每一個servlet請求建立一個新的java線程。針對用戶的請求,若是servlet已經運行完相應的http服務方法,這個線程就會結束。這並非說你不須要考慮線程安全性,其實你還會遇到同步問題,不過這樣能使你少作不少工做。
聲明方式實現安全:利用servlet容器,能夠使用xml部署描述文件來配置和修改安全性,而沒必要將其硬編碼寫到servlet類代碼中。
JSP支持:servlet容器負責將jsp代碼翻譯爲真正的java代碼。
Spring容器
Spring容器是管理service和dao的。
SpringMVC容器
SpringMVC容器是管理controller對象的。
Spring容器和SpringMVC容器的關係是父子容器的關係。Spring容器是父容器,SpringMVC容器是子容器。在子容器裏能夠訪問父容器裏的對象,可是在父容器裏不能夠訪問子容器的對象,說的通俗點就是,在controller裏能夠訪問service對象,可是在service裏不能夠訪問controller對象。因此這麼看的話,全部的bean,都是被Spring或者SpringMVC容器管理的,他們能夠直接注入。而後SpringMVC的攔截器也是SpringMVC容器管理的,因此在SpringMVC的攔截器裏,能夠直接注入bean對象。
Servlet容器和ServletContext的關係:
ServletContext是servlet與servlet容器之間的直接通訊的接口。Servlet容器在啓動一個Web應用時,會爲它建立一個servletContext對象。每一個web應用有惟一的servletContext對象。同一個web應用的全部servlet對象共享一個serveltContext,servlet對象能夠經過它來訪問容器中的各類資源。
各個容器的建立過程:
1、TOMCAT啓動,Servlet容器隨即啓動,而後讀取server.xml配置文件,啓動裏面配置的web應用,爲每一個應用建立一個「全局上下文環境」(ServletContext);
2、建立Spring容器實例。調用web.xml中配置的ContextLoaderListener,初始化WebApplicationContext上下文環境(即IOC容器),加載contextparam指定的配置文件信息到IOC容器中。WebApplicationContext在ServletContext中以鍵值對的形式保存。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:root-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
三、 建立SpringMVC容器實例。調用web.xml中配置的servlet-class,爲其初始化本身的上下文信息,並加載其設置的配置信息到該上下文中。將WebApplicationContext設置爲它的父容器。
<!-- springMVC配置 --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:servlet-context.xml</param-value> </init-param>
<init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name > <param-value>true </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
4、此後的全部servlet的初始化都按照3步中方式建立,初始化本身的上下文環境,將WebApplicationContext設置爲本身的父上下文環境。當Spring在執行ApplicationContext的getBean時,若是在本身context中找不到對應的bean,則會在父ApplicationContext中去找。