閱讀前提:
1、理解IOC的一些概念,以及在Spring中的實(shí)現(xiàn)(上下文,BeanFactory,BeanDefinition等等)
2、理解Web的基礎(chǔ)知識(shí)(Servlet)
3、理解MVC的基礎(chǔ)知識(shí)(Model/View/Controller)
spring mvc的配置解析:
一、需要在web.xml中配置DispatchServlet
<!-- Spring MVC 配置 -->
<servlet>
<servlet-name>spring-sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring bean配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目錄。默認(rèn)配置在WEB-INF目錄下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
1.servlet節(jié)點(diǎn)描述的是servlet對(duì)象,這里是spring mvc的DispatcherServlet,servlet的名稱為spring-sample
2.servlet-mapping節(jié)點(diǎn)描述的是url映射,這里定義了對(duì)于任何/*的請(qǐng)求都走名稱為spring-sample的servlet去處理
3.listener節(jié)點(diǎn)描述的spring mvc的啟動(dòng)類,ContextLoaderListener被定義為一個(gè)監(jiān)聽器,負(fù)責(zé)完成IoC容器在web環(huán)境中的啟動(dòng)
4.context-param節(jié)點(diǎn)描述的是spring的bean配置文件
5.servlet的load-on-startup作用:
關(guān)于load-on-startup的官方描述
Servlet specification: The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value
load-on-startup指明當(dāng)web應(yīng)用啟動(dòng)的時(shí)候,初始化servlet(調(diào)用init()方法),
1)當(dāng)值為0或者大于0時(shí),表示容器在應(yīng)用啟動(dòng)時(shí)就加載并初始化這個(gè)servlet;
2)當(dāng)值小于0或者沒有指定時(shí),則表示容器在該servlet被選擇時(shí)才會(huì)去加載;
3)值越小,該servlet的優(yōu)先級(jí)越高,應(yīng)用啟動(dòng)時(shí)就越先加載;
二、需要在bean定義中配置web請(qǐng)求和Controller的對(duì)應(yīng)關(guān)系,例如常用的通過RequestMapping注解來(lái)定義web請(qǐng)求和Controller的對(duì)應(yīng)關(guān)系
@RequestMapping("/action")
public ModelAndView action(
......
)
spring mvc源碼解析:
spring mvc主要涉及2個(gè)模塊
- spring-web
- spring-webmvc
mvc核心概念
- view:視圖
- model:模型
- controller:控制器
spring mvc核心實(shí)體類/接口
- View:視圖
- Handler:對(duì)應(yīng)mvc中的控制器
- HandlerAdapter:handler的調(diào)度器
- DispatcherServlet:http的請(qǐng)求分發(fā)處理器
- ModelAndView:handler返回的model和view對(duì)象
spring mvc核心類源碼解讀:
- ContextLoaderListener:
源碼位置:
決定第一個(gè)講ContextLoaderListener,是因?yàn)镃ontextLoaderListener負(fù)責(zé)在項(xiàng)目啟動(dòng)時(shí)掃描spring的Bean配置(比如applicationContext.xml),加載Bean的定義到IoC容器中去,是spring mvc應(yīng)用的基礎(chǔ)
ContextLoaderListener的源碼如下:
/**
* Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.
* Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.
*
* <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web
* application context via the {@link #ContextLoaderListener(WebApplicationContext)}
* constructor, allowing for programmatic configuration in Servlet 3.0+ environments.
* See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 17.02.2003
* @see #setContextInitializers
* @see org.springframework.web.WebApplicationInitializer
*/
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
public ContextLoaderListener() {
}
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
/**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
ContextLoaderListener的核心:
1.讀類注釋可以知道ContextLoaderListener的作用是負(fù)責(zé)啟動(dòng)和關(guān)閉spring的root webapplicationContext 2.ContextLoaderListener繼承了ServletContextListener接口,重寫了contextInitialized和contextDestroyed方法,這2個(gè)方法是和servlet生命周期相結(jié)合的回調(diào),所以ServletContextListener的功能之一是對(duì)servlet的生命周期做監(jiān)聽 3.繼承了ContextLoader類,通過調(diào)用ContextLoader類中的initWebApplicationContext()/closeWebApplicationContext()2個(gè)方法實(shí)現(xiàn)對(duì)root webapplicationContext的初始化和銷毀
- ContextLoader
initWebApplicationContext()/closeWebApplicationContext()的實(shí)現(xiàn):
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
public void closeWebApplicationContext(ServletContext servletContext) {
servletContext.log("Closing Spring root WebApplicationContext");
try {
if (this.context instanceof ConfigurableWebApplicationContext) {
((ConfigurableWebApplicationContext) this.context).close();
}
}
finally {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = null;
}
else if (ccl != null) {
currentContextPerThread.remove(ccl);
}
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
}
}