歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring中MVC框架的底層實現

Spring中MVC框架的底層實現

日期:2017/3/1 10:38:24   编辑:Linux編程

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. // Configure the bean definition reader with this context's
  12. // resource loading environment.
  13. beanDefinitionReader.setEnvironment(this.getEnvironment());
  14. beanDefinitionReader.setResourceLoader(this);
  15. beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
  16. // Allow a subclass to provide custom initialization of the reader,
  17. // then proceed with actually loading the bean definitions.
  18. initBeanDefinitionReader(beanDefinitionReader);
  19. loadBeanDefinitions(beanDefinitionReader);
  20. }
Copyright © Linux教程網 All Rights Reserved