国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

傳統tomcat啟動(dòng)服務(wù)與springboot啟動(dòng)內置tomcat服務(wù)的

發(fā)布時(shí)間:2021-08-15 18:37 來(lái)源: 閱讀:0 作者:安安靜靜寫(xiě)bug 欄目: 服務(wù)器 歡迎投稿:712375056

spring整合springmvc

  •  spring整合springmvc中web.xml配置如下,tomcat在啟動(dòng)過(guò)程中會(huì )加載web.xml中的內容,ContextLoaderListener實(shí)現了tomcat里面的ServletContextListener接口,所以在tomcat容器啟動(dòng)過(guò)程通過(guò)ContextLoaderListener來(lái)進(jìn)行spring容器的初始化操作,并將classpath:spring/applicationContext-*.xml指定下的spring配置文件加載,該配置文件我只配置了<context:component-scan base-package=“org.com.yp”/>,代表通過(guò)掃描org.com.yp包下的類(lèi),包含@Component @Controller@Service等注解等類(lèi),進(jìn)行bean注冊。
  • bean注冊是通過(guò)AbstractXmlApplicationContext.loadBeanDefinitions該類(lèi)的方法進(jìn)行bean定義加載的。

spring中加載bean定義是在org.springframework.context.ConfigurableApplicationContext#refresh方法中的ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()方法加載bean的,該方法之后會(huì )調用org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory方法創(chuàng )建bean工廠(chǎng),并加載的bean定義。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 加載spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
​
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置springMVC需要加載的配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
  </servlet>
​
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <!-- 默認匹配所有的請求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

當tomcat容器啟動(dòng)后,通過(guò)路徑訪(fǎng)問(wèn)資源時(shí),第一次會(huì )調用org.springframework.web.servlet.HttpServletBean#init方法,之后的http請求就不會(huì )再方法該方法類(lèi);HttpServletBean實(shí)現了Servlet接口的規范,所以經(jīng)過(guò)瀏覽器的請求經(jīng)過(guò)servlet接口初始化執行init方法時(shí),會(huì )再從spring容器中去加載springmvc配置中定義的加載類(lèi),spring與springmvc是父子容器的關(guān)系,下面是HttpServletBean的init方法

public final void init() throws ServletException {
		// Set bean properties from init parameters.
		PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
		if (!pvs.isEmpty()) {
			try {
				BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
				ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
				bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
				initBeanWrapper(bw);
				bw.setPropertyValues(pvs, true);
			}
			catch (BeansException ex) {
				if (logger.isErrorEnabled()) {
					logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
				}
				throw ex;
			}
		}

        // 最后會(huì )調用org.springframework.context.ConfigurableApplicationContext#refresh容器的刷新方法,
        // 進(jìn)行springmvc容器初始化
		initServletBean();
	}
    }

springboot啟動(dòng)容器

  •  springboot啟動(dòng)的方式則是先在springboot的org.springframework.boot.SpringApplication#run(java.lang.String…)方法中就初始化了spring的上下文環(huán)境(里面包含bean工廠(chǎng)),之后通過(guò)org.springframework.boot.SpringApplication#refreshContext方法調用Spring容器中的ConfigurableApplicationContext#refresh方法初始化bean.
  • 在spring與springmvc整合的環(huán)境中,bean定義的加載是在org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory方法,而springboot中是在

org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors方法,該方法中通過(guò)ConfigurationClassPostProcessor類(lèi)去加載bean定義,該類(lèi)實(shí)現了BeanDefinitionRegistryPostProcessor接口,這個(gè)接口允許對bean定義進(jìn)行加工處理。

// spring中的BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,
// BeanFactoryPostProcessor的作用是在bean的定義信息已經(jīng)加載但還沒(méi)有初始化的時(shí)候執行方法postProcessBeanFactory()方法,
// 而B(niǎo)eanDefinitionRegistryPostProcessor是在BeanFactoryPostProcessor的前面執行,在源碼
// org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors()方法里面定義了執行順序
// BeanFactoryPostProcessor是bean工廠(chǎng)的bean屬性處理容器,說(shuō)通俗一些就是可以管理我們的bean工廠(chǎng)內所有的beandefinition(未實(shí)例化)數據,可以隨心所欲的修改屬性。
public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            prepareRefresh();
            
            //獲取告訴子類(lèi)初始化Bean工廠(chǎng) 將bean加載到緩存中 spring springmvc整合是在這初始化bean的
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
            
            prepareBeanFactory(beanFactory);
​
            try {
                postProcessBeanFactory(beanFactory);
​
                // springboot容器啟動(dòng)加載到這時(shí),初始化了下面幾個(gè)bean name
                //0 = "org.springframework.context.annotation.internalConfigurationAnnotationProcessor" =》對應ConfigurationClassPostProcessor類(lèi)
                //1 = "org.springframework.context.annotation.internalAutowiredAnnotationProcessor" =》 AutowiredAnnotationBeanPostProcessor
                //2 = "org.springframework.context.annotation.internalCommonAnnotationProcessor" =》 CommonAnnotationBeanPostProcessor
                //3 = "org.springframework.context.event.internalEventListenerProcessor" =》 EventListenerMethodProcessor
                //4 = "org.springframework.context.event.internalEventListenerFactory" =》 DefaultEventListenerFactory
                // 調用我們的bean工廠(chǎng)的后置處理器.加載bean定義(不是實(shí)例化),通過(guò)ConfigurationClassPostProcessor去加載啟動(dòng)類(lèi)中的掃描路徑
                // 然后將路徑下到bean加載進(jìn)來(lái)
                invokeBeanFactoryPostProcessors(beanFactory);
​
                registerBeanPostProcessors(beanFactory);
​
                initMessageSource();
​
                initApplicationEventMulticaster();
​
                // 這個(gè)方法同樣也是留個(gè)子類(lèi)實(shí)現的springboot也是從這個(gè)方法進(jìn)行啟動(dòng)tomat的.
                onRefresh();
​
                registerListeners();
​
                //實(shí)例化我們剩余的單實(shí)例bean.
                finishBeanFactoryInitialization(beanFactory);
​
                // 最后容器刷新 發(fā)布刷新事件(Spring cloud也是從這里啟動(dòng)的)
                finishRefresh();
            }
​
            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception  encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }
​
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();
​
                // Reset 'active' flag.
                cancelRefresh(ex);
​
                // Propagate exception to caller.
                throw ex;
            }
​
            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

到此這篇關(guān)于傳統tomcat啟動(dòng)服務(wù)與springboot啟動(dòng)內置tomcat服務(wù)的區別的文章就介紹到這了,更多相關(guān)tomcat啟動(dòng)服務(wù)與springboot啟動(dòng)內置tomcat服務(wù)區別內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

亚洲综合色丁香婷婷六月图片| 国产成人综合久久免费| 久久久精品国产SM调教网站| 国产一精品一AV一免费| 亚洲熟妇色XXXXX欧美老妇| 无遮无挡爽爽免费毛片|