網頁

2018/3/11

Spring MVC配置檔載入流程

今天突然對Spring MVC在web.xmlDispatcherServlet<servlet-name>和Spring MVC設定檔的名稱及位置是怎麼決定的,所以跑去看原始碼,下面是今天看了一整天大概了解的流程,在此紀錄一下。

但找了半天還是找不到contextConfigLocation是在哪個地方被設定為Spring MVC配置檔的路徑?

-Catalina.start() //Tomcat引擎Catalina啟動
 -Server.start()(LifecycleBase.start())
  -StandardardContext.startInternal()
    -StandardardContext.fireLifecycleEvent()
     -LifecycleListener.lifecycleEvent() (ContextConfig.lifecycleEvent())
      -ContextConfig.configureStart()
       -ContextConfig.webConfig() //解析web.xml
        -WebXml.merge()
         -WebXml.mergeServlet()
          -ServletDef.addInitParameter() 取得<init-param>的設定,例如contextConfigLocation
        -ContextConfig.configureContext()
         -ServletDef.getParameterMap() 取得參數的設定,例如contextConfigLocation
         -StandardardWrapper.addInitParameter(String name, String value) //將Servlet參數放入StandardardWrapper.parameters <---+
         -StandardardWrapper.setServletClass() //加入web.xml中的Servlet名稱,一個ServletDef會有一個Wrapper                     |
        -StandardardContext.addServletContainerInitializer()                                                                 |
    -ServletContainerInitializer.onStartup()(SpringServletContainerInitializer.onStartup()) //以程式來設定config              |
     -AbstractDispatcherServletInitializer.onStartup()                                                                       |
      -AbstractDispatcherServletInitializer.registerDispatcherServlet()                                                      |
       -AbstractDispatcherServletInitializer.createServletApplicationContext()                                               |
       -AbstractDispatcherServletInitializer.createDispatcherServlet()                                                       |
       -ServletContext.addServlet()                                                                                          |
    -StandardardContext.loadOnStartup()                                                                                      |
     -StandardardWrapper.load()                                                                                              |
      -StandardardWrapper.loadServlet()                                                                                      |
       -InstanceManager.newInstance() (DefaultInstanceManager.newInstance()) //根據 web.xml來產生Servlet的實例                |
       -StandardWrapper.initServlet() //初始化Servlet                                                                        |
        -Servlet.init()(GenericServlet.init())                                                                               |
         -HttpServletBean.init()                                                                                             |
          -new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties)                                      |
           -StandardardWrapper.getInitParameter(String name)                                                                 |
            -StandardardWrapper.findInitParameter(String name) //取出Servlet參數 <-------------------------------------------+
           -BeanWrapper.setPropertyValues(PropertyValues pvs, boolean ignoreUnknown)(AbstractPropertyAccessor.setPropertyValues(PropertyValues pvs, boolean ignoreUnknown))
            -AbstractPropertyAccessor.setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid)
             -AbstractPropertyAccessor.setPropertyValue(PropertyValue pv)
              -AbstractNestablePropertyAccessor.setPropertyValue(String propertyName, Object value)
               -AbstractNestablePropertyAccessor.setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv)
          -HttpServletBean.initServletBean()(FrameworkServlet.initServletBean())
           -FrameworkServlet.initWebApplicationContext()
            -FrameworkServlet.createWebApplicationContext()
             -FrameworkServlet.createWebApplicationContext() //建立WebApplicationContext (預設DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext)
              -FrameworkServlet.getContextClass() 
               -return contextClass = DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class
              -BeanUtils.instantiateClass()
               -BeanUtils.instantiateClass()
              -FrameworkServlet.getContextConfigLocation() 
              -FrameworkServlet.setConfigLocation()
             -FrameworkServlet.configureAndRefreshWebApplicationContext()
              -FrameworkServlet.applyInitializers()
               -ApplicationContextInitializer.initialize()
              -ConfigurableWebApplicationContext.refresh()(AbstractApplicationContext.refresh())
               -ConfigurableListableBeanFactory.obtainFreshBeanFactory()
                -AbstractRefreshableApplicationContext.refreshBeanFactory()
                 -XmlWebApplicationContext.loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
                  -XmlWebApplicationContext.loadBeanDefinitions(XmlBeanDefinitionReader reader)
                   -AbstractRefreshableWebApplicationContext.getConfigLocations()() 
                    -AbstractRefreshableConfigApplicationContext.getConfigLocations() //取得contextConfigLocation路徑
                     -return configLocations // 不知在哪被set?
                     -XmlWebApplicationContext.getDefaultConfigLocations()
                      -return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX} //Spring MVC配置檔預設會根據DispatcherServlet的servlet-name前後加上"/WEB-INF/"及".xml"
                       -DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"
                       -DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
                      -return new String[] {DEFAULT_CONFIG_LOCATION}
                       -DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; //Spring MVC配置檔預設路徑名稱
                   -XmlBeanDefinitionReader.loadBeanDefinitions(String location)
                    -AbstractBeanDefinitionReader.loadBeanDefinitions(String location, Set actualResources)
                     -AbstractBeanDefinitionReader.loadBeanDefinitions(Resource... resources)
                      -XmlBeanDefinitionReader.loadBeanDefinitions(Resource resource)
                       -XmlBeanDefinitionReader.loadBeanDefinitions(EncodedResource encodedResource)
                        -XmlBeanDefinitionReader.doLoadBeanDefinitions(InputSource inputSource, Resource resource)
                         -XmlBeanDefinitionReader.registerBeanDefinitions(Document doc, Resource resource)
                          -DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(Document doc, XmlReaderContext readerContext)
                           -DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(Element root)
                            -DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) //解析spring設定檔的bean
                             -DefaultBeanDefinitionDocumentReader.parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate)
                              -DefaultBeanDefinitionDocumentReader.processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate)
                               -BeanDefinitionReaderUtils.registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
                                -DefaultListableBeanFactory.registerBeanDefinition(String beanName, BeanDefinition beanDefinition) //註冊bean

沒有留言:

張貼留言