spring整合springmvc spring整合springmvc中web.xml配置如下,tomcat在啟動(dòng)過程中會(huì)加載web.xml中的內(nèi)容,ContextLoaderListener實(shí)現(xiàn)了tomcat里面的ServletContextListener接口,所以在tomcat容器啟動(dòng)過程通過ContextLoaderListener來進(jìn)行spring容器的初始化操作,并將classpath:spring/applicationContext-*.xml指定下的spring配置文件加載,該配置文件我只配置了
新沂網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)從2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
<?xml version="1.0" encoding="UTF-8"?>
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ì)調(diào)用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工廠),之后通過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接口,這個(gè)接口允許對(duì)bean定義進(jìn)行加工處理。// spring中的BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,// BeanFactoryPostProcessor的作用是在bean的定義信息已經(jīng)加載但還沒有初始化的時(shí)候執(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容器啟動(dòng)加載到這時(shí),初始化了下面幾個(gè)bean name //0 = "org.springframework.context.annotation.internalConfigurationAnnotationProcessor" =》對(duì)應(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去加載啟動(dòng)類中的掃描路徑 // 然后將路徑下到bean加載進(jìn)來 invokeBeanFactoryPostProcessors(beanFactory); registerBeanPostProcessors(beanFactory); initMessageSource(); initApplicationEventMulticaster(); // 這個(gè)方法同樣也是留個(gè)子類實(shí)現(xiàn)的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)于傳統(tǒng)tomcat啟動(dòng)服務(wù)與springboot啟動(dòng)內(nèi)置tomcat服務(wù)的區(qū)別的文章就介紹到這了,更多相關(guān)tomcat啟動(dòng)服務(wù)與springboot啟動(dòng)內(nèi)置tomcat服務(wù)區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!