Spring中MVC框架的底层实现


Spring中的MVC

Spring MVC的流程

Spring的Sample这里就不讲了,大家自己上网google

Spring 在Web环境下的启动

                按照javaEE标准,Web应用启动web.xml,之后的启动root是ServletContextListener。

                (问题: ContextListener如何启动? 随着web容器启动而启动?是单线程的?线程安全的?)

ContextListener是随着Tomcat的启动而启动,并且只启动这一次,为整个WebContext的启动做准备。

                Spring在Web环境下启动的监听器是:

[java]
  1. public class ContextLoaderListener extends ContextLoader implements ServletContextListener {  
  2. /** 
  3.      * Initialize the root web application context. 
  4.      */  
  5.     public void contextInitialized(ServletContextEvent event) {  
  6.         this.contextLoader = createContextLoader();  
  7.         if (this.contextLoader == null) {  
  8.             this.contextLoader = this;  
  9.         }  
  10.         this.contextLoader.initWebApplicationContext(event.getServletContext());  
  11.     }  

其中WebApplication的上下文在ContextLoader中初期化

[java]
  1. /** 
  2.      * Initialize Spring's web application context for the given servlet context, 
  3.      * using the application context provided at construction time, or creating a new one 
  4.      * according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and 
  5.      * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params. 
  6.      * @param servletContext current servlet context 
  7.      * @return the new WebApplicationContext 
  8.      * @see #ContextLoader(WebApplicationContext) 
  9.      * @see #CONTEXT_CLASS_PARAM 
  10.      * @see #CONFIG_LOCATION_PARAM 
  11.      */  
  12.     public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {  
  13. try {  
  14.             // Store context in local instance variable, to guarantee that   
  15.             // it is available on ServletContext shutdown.   
  16.             if (this.context == null) {  
  17.                 this.context = createWebApplicationContext(servletContext);  
  18.             }     

最后的启动委托给XmlWebApplicationContext

这个类中使用了大量的模板设计模式!!

最终的容器启动和我们编程式启动Spring类同

[java]
  1. /** 
  2.      * Loads the bean definitions via an XmlBeanDefinitionReader. 
  3.      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 
  4.      * @see #initBeanDefinitionReader 
  5.      * @see #loadBeanDefinitions 
  6.      */  
  7.     @Override  
  8.     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {  
  9.         // Create a new XmlBeanDefinitionReader for the given BeanFactory.   
  10.         XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  
  11.   
  12.         // Configure the bean definition reader with this context's   
  13.         // resource loading environment.   
  14.         beanDefinitionReader.setEnvironment(this.getEnvironment());  
  15.         beanDefinitionReader.setResourceLoader(this);  
  16.         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));  
  17.   
  18.         // Allow a subclass to provide custom initialization of the reader,   
  19.         // then proceed with actually loading the bean definitions.   
  20.         initBeanDefinitionReader(beanDefinitionReader);  
  21.         loadBeanDefinitions(beanDefinitionReader);  
  22.     }  
  • 1
  • 2
  • 3
  • 下一页

相关内容