閱讀前提:
1灌闺、理解IOC的一些概念萤衰,以及在Spring中的實(shí)現(xiàn)(上下文堕义,BeanFactory,BeanDefinition等等)
2腻菇、理解Web的基礎(chǔ)知識(Servlet)
3胳螟、理解MVC的基礎(chǔ)知識(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對象糖耸,這里是spring mvc的DispatcherServlet,servlet的名稱為spring-sample
2.servlet-mapping節(jié)點(diǎn)描述的是url映射丘薛,這里定義了對于任何/*的請求都走名稱為spring-sample的servlet去處理
3.listener節(jié)點(diǎn)描述的spring mvc的啟動類嘉竟,ContextLoaderListener被定義為一個監(jiān)聽器,負(fù)責(zé)完成IoC容器在web環(huán)境中的啟動
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)用啟動的時候洋侨,初始化servlet(調(diào)用init()方法)舍扰,
1)當(dāng)值為0或者大于0時,表示容器在應(yīng)用啟動時就加載并初始化這個servlet希坚;
2)當(dāng)值小于0或者沒有指定時边苹,則表示容器在該servlet被選擇時才會去加載;
3)值越小裁僧,該servlet的優(yōu)先級越高个束,應(yīng)用啟動時就越先加載;
二聊疲、需要在bean定義中配置web請求和Controller的對應(yīng)關(guān)系茬底,例如常用的通過RequestMapping注解來定義web請求和Controller的對應(yīng)關(guān)系
@RequestMapping("/action")
public ModelAndView action(
......
)
spring mvc源碼解析:
spring mvc主要涉及2個模塊
- spring-web
- spring-webmvc
mvc核心概念
- view:視圖
- model:模型
- controller:控制器
spring mvc核心實(shí)體類/接口
- View:視圖
- Handler:對應(yīng)mvc中的控制器
- HandlerAdapter:handler的調(diào)度器
- DispatcherServlet:http的請求分發(fā)處理器
- ModelAndView:handler返回的model和view對象
spring mvc核心類源碼解讀:
- ContextLoaderListener:
源碼位置:
決定第一個講ContextLoaderListener,是因?yàn)镃ontextLoaderListener負(fù)責(zé)在項(xiàng)目啟動時掃描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é)啟動和關(guān)閉spring的root webapplicationContext 2.ContextLoaderListener繼承了ServletContextListener接口,重寫了contextInitialized和contextDestroyed方法,這2個方法是和servlet生命周期相結(jié)合的回調(diào)最爬,所以ServletContextListener的功能之一是對servlet的生命周期做監(jiān)聽 3.繼承了ContextLoader類涉馁,通過調(diào)用ContextLoader類中的initWebApplicationContext()/closeWebApplicationContext()2個方法實(shí)現(xiàn)對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);
}
}