濮阳杆衣贸易有限公司

主頁 > 知識庫 > 傳統(tǒng)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)的區(qū)別(推薦)

傳統(tǒng)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)的區(qū)別(推薦)

熱門標(biāo)簽:怎么找到?jīng)]有地圖標(biāo)注的店 打400電話怎么辦理收費(fèi) 上海申請高400開頭的電話 400電話辦理介紹信 宿城區(qū)電話機(jī)器人找哪家 河南防封號電銷機(jī)器人是什么 麗江真人語音電話外呼系統(tǒng) 10086外包用的什么外呼系統(tǒng) 福州企業(yè)電銷機(jī)器人排名

spring整合springmvc

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

spring中加載bean定義是在org.springframework.context.ConfigurableApplicationContext#refresh方法中的ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()方法加載bean的,該方法之后會調(diào)用org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory方法創(chuàng)建bean工廠,并加載的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>
    <!-- 默認(rèn)匹配所有的請求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

當(dāng)tomcat容器啟動后,通過路徑訪問資源時,第一次會調(diào)用org.springframework.web.servlet.HttpServletBean#init方法,之后的http請求就不會再方法該方法類;HttpServletBean實(shí)現(xiàn)了Servlet接口的規(guī)范,所以經(jīng)過瀏覽器的請求經(jīng)過servlet接口初始化執(zhí)行init方法時,會再從spring容器中去加載springmvc配置中定義的加載類,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;
			}
		}

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

springboot啟動容器

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

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

// spring中的BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,
// BeanFactoryPostProcessor的作用是在bean的定義信息已經(jīng)加載但還沒有初始化的時候執(zhí)行方法postProcessBeanFactory()方法,
// 而BeanDefinitionRegistryPostProcessor是在BeanFactoryPostProcessor的前面執(zhí)行,在源碼
// org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors()方法里面定義了執(zhí)行順序
// BeanFactoryPostProcessor是bean工廠的bean屬性處理容器,說通俗一些就是可以管理我們的bean工廠內(nèi)所有的beandefinition(未實(shí)例化)數(shù)據(jù),可以隨心所欲的修改屬性。
public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            prepareRefresh();
            
            //獲取告訴子類初始化Bean工廠 將bean加載到緩存中 spring springmvc整合是在這初始化bean的
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
            
            prepareBeanFactory(beanFactory);
​
            try {
                postProcessBeanFactory(beanFactory);
​
                // springboot容器啟動加載到這時,初始化了下面幾個bean name
                //0 = "org.springframework.context.annotation.internalConfigurationAnnotationProcessor" =》對應(yīng)ConfigurationClassPostProcessor類
                //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
                // 調(diào)用我們的bean工廠的后置處理器.加載bean定義(不是實(shí)例化),通過ConfigurationClassPostProcessor去加載啟動類中的掃描路徑
                // 然后將路徑下到bean加載進(jìn)來
                invokeBeanFactoryPostProcessors(beanFactory);
​
                registerBeanPostProcessors(beanFactory);
​
                initMessageSource();
​
                initApplicationEventMulticaster();
​
                // 這個方法同樣也是留個子類實(shí)現(xiàn)的springboot也是從這個方法進(jìn)行啟動tomat的.
                onRefresh();
​
                registerListeners();
​
                //實(shí)例化我們剩余的單實(shí)例bean.
                finishBeanFactoryInitialization(beanFactory);
​
                // 最后容器刷新 發(fā)布刷新事件(Spring cloud也是從這里啟動的)
                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)于傳統(tǒng)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)的區(qū)別的文章就介紹到這了,更多相關(guān)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:雞西 面試通知 荊門 朝陽 連云港 隴南 遵義 運(yùn)城

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《傳統(tǒng)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)的區(qū)別(推薦)》,本文關(guān)鍵詞  傳統(tǒng),tomcat,啟動,服務(wù),與,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《傳統(tǒng)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)的區(qū)別(推薦)》相關(guān)的同類信息!
  • 本頁收集關(guān)于傳統(tǒng)tomcat啟動服務(wù)與springboot啟動內(nèi)置tomcat服務(wù)的區(qū)別(推薦)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    河津市| 六盘水市| 察雅县| 花莲市| 长汀县| 钟祥市| 赞皇县| 玉环县| 阿图什市| 乐业县| 乐昌市| 兴山县| 中牟县| 延长县| 宝坻区| 金坛市| 铁岭县| 台北市| 甘孜县| 益阳市| 凤阳县| 怀远县| 龙江县| 正阳县| 云南省| 泸定县| 三门峡市| 无为县| 张家港市| 都江堰市| 丁青县| 喀什市| 庆元县| 宽城| 盖州市| 临邑县| 辉县市| 新巴尔虎左旗| 凭祥市| 扬中市| 蓬溪县|