Spring框架:第九章:Spring整合Web
Spring整合Web
在web工程中添加Spring的jar包。
Spring的核心包
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
aop包
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
JDBC-ORM包
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
Spring的web整合包
spring-web-4.0.0.RELEASE.jar
測(cè)試包
spring-test-4.0.0.RELEASE.jar
整合Spring和Web容器分兩個(gè)步驟:
1、導(dǎo)入spring-web-4.0.0.RELEASE.jar
2、在web.xml配置文件中配置org.springframework.web.context.ContextLoaderListener監(jiān)聽(tīng)器監(jiān)聽(tīng)ServletContext的初始化
3、在web.xml配置文件中配置contextConfigLocation上下文參數(shù)。配置Spring配置文件的位置,以用于初始化Spring容器
在web.xml中配置
<!-- needed for ContextLoaderListener -->
<!--配置SpringIOC容器的配置文件路徑-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>location</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<!--這個(gè)監(jiān)聽(tīng)器會(huì)在web工程啟動(dòng)時(shí)候創(chuàng)建Spring IOC 容器。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
這個(gè)監(jiān)聽(tīng)器會(huì)在web工程啟動(dòng)時(shí)候讀取spring的配置文件,做初始化Spring IOC 容器的操作。
這個(gè)Spring IOC 容器對(duì)象保存到ServletContext域?qū)ο笾形覀兙涂梢允褂肧pring IOC容器對(duì)象。
我們可以深入進(jìn)去看看底層源碼:
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
可以看到它實(shí)現(xiàn)了ServletContextListener,ServletContextListener監(jiān)聽(tīng)servletContext對(duì)象的創(chuàng)建和銷毀
在ServletContextListener里可以看到有contextInitialized和contextDestroyed
// Method descriptor #5 (Ljavax/servlet/ServletContextEvent;)V
public abstract void contextInitialized(javax.servlet.ServletContextEvent arg0);
// Method descriptor #5 (Ljavax/servlet/ServletContextEvent;)V
public abstract void contextDestroyed(javax.servlet.ServletContextEvent arg0);
contextInitialized是servletContext對(duì)象的創(chuàng)建
contextDestroyed是servletContext對(duì)象的銷毀
servletContext對(duì)象的創(chuàng)建在web工程啟動(dòng)的時(shí)候創(chuàng)建,web工程啟動(dòng)的時(shí)候需要有一個(gè)springIOC容器
可以看看我們之前的代碼@ContextConfiguration(locations = “classpath:applicationContext.xml”)或者
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);都創(chuàng)建了Spring的IOC容器對(duì)象
在創(chuàng)建了Spring的IOC容器對(duì)象時(shí)我們需要Spring的配置文件:applicationContext.xml
我們?cè)趙eb.xml中配置
<!--配置SpringIOC容器的配置文件路徑-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
下面這個(gè)監(jiān)聽(tīng)器一啟動(dòng)就會(huì)讀取上面的參數(shù):配置文件路徑
<!--這個(gè)監(jiān)聽(tīng)器會(huì)在web工程啟動(dòng)時(shí)候創(chuàng)建Spring IOC 容器。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我們可以使用DeBug看源碼ContextLoaderListener的initWebApplicationContext(event.getServletContext());找到ContextLoader中initWebApplicationContext方法
createWebApplicationContext創(chuàng)建一個(gè)Spring容器對(duì)象,可以看到它有值了
可以繼續(xù)往下看configureAndRefreshWebApplicationContext配置和刷新Spring容器
我們進(jìn)入它的方法體內(nèi)找到以下代碼
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
它來(lái)讀取web.xml中<param-name>contextConfigLocation</param-name>的值,獲得初始化參數(shù),得到我們自己寫的<param-value>classpath:applicationContext.xml</param-value>
把它放到Spring容器中
執(zhí)行到wac.refresh();位置就開(kāi)始刷新,執(zhí)行后要等上一段時(shí)間,執(zhí)行完這段代碼后,我們的配置文件applicationContext.xml已經(jīng)全部加載好了,bean對(duì)象也創(chuàng)建好了。
回到initWebApplicationContext方法中執(zhí)行到
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
其中this.context
它把容器對(duì)象放到了servletContext域?qū)ο笾?,使用在web工程中只要獲取到servletContext對(duì)象就可以在servletContext對(duì)象中獲取spring容器對(duì)象
這就是在web.xml中配置的底層原理
我們?nèi)绾潍@取到spring容器對(duì)象也就是WebApplicationContext呢?
獲取WebApplicationContext上下文對(duì)象的方法如下:
方法一(不推薦):
getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
這一個(gè)好理解,前面我們?nèi)萜鲗?duì)象放到了servletContext域?qū)ο笾?,現(xiàn)在獲取就好了,不過(guò)后面的一長(zhǎng)串不好記憶,所以不推薦
方法二(推薦):
WebApplicationContextUtils.getWebApplicationContext(getServletContext())
使用這種的就好些了,不用我們記憶,而且它的底層和方法一是一樣的,也是獲取getAttribute