栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > 服务器

传统tomcat启动服务与springboot启动内置tomcat服务的区别(推荐)

服务器 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

传统tomcat启动服务与springboot启动内置tomcat服务的区别(推荐)

spring整合springmvc
  •  spring整合springmvc中web.xml配置如下,tomcat在启动过程中会加载web.xml中的内容,ContextLoaderListener实现了tomcat里面的ServletContextListener接口,所以在tomcat容器启动过程通过ContextLoaderListener来进行spring容器的初始化操作,并将classpath:spring/applicationContext-*.xml指定下的spring配置文件加载,该配置文件我只配置了,代表通过扫描org.com.yp包下的类,包含@Component @Controller@Service等注解等类,进行bean注册。
  • bean注册是通过AbstractXmlApplicationContext.loadBeanDefinitions该类的方法进行bean定义加载的。

spring中加载bean定义是在org.springframework.context.ConfigurableApplicationContext#refresh方法中的ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()方法加载bean的,该方法之后会调用org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory方法创建bean工厂,并加载的bean定义。



  Archetype Created Web Application
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    contextConfigLocation
    classpath:spring/applicationContext-*.xml
  
    mvc-dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring/spring-*.xml
    
  
    mvc-dispatcher
    
    /
  

当tomcat容器启动后,通过路径访问资源时,第一次会调用org.springframework.web.servlet.HttpServletBean#init方法,之后的http请求就不会再方法该方法类;HttpServletBean实现了Servlet接口的规范,所以经过浏览器的请求经过servlet接口初始化执行init方法时,会再从spring容器中去加载springmvc配置中定义的加载类,spring与springmvc是父子容器的关系,下面是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;
			}
		}

 // 最后会调用org.springframework.context.ConfigurableApplicationContext#refresh容器的刷新方法,
 // 进行springmvc容器初始化
		initServletBean();
	}
    }

springboot启动容器

  •  springboot启动的方式则是先在springboot的org.springframework.boot.SpringApplication#run(java.lang.String…)方法中就初始化了spring的上下文环境(里面包含bean工厂),之后通过org.springframework.boot.SpringApplication#refreshContext方法调用Spring容器中的ConfigurableApplicationContext#refresh方法初始化bean.
  • 在spring与springmvc整合的环境中,bean定义的加载是在org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory方法,而springboot中是在

org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors方法,该方法中通过ConfigurationClassPostProcessor类去加载bean定义,该类实现了BeanDefinitionRegistryPostProcessor接口,这个接口允许对bean定义进行加工处理。

// spring中的BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,
// BeanFactoryPostProcessor的作用是在bean的定义信息已经加载但还没有初始化的时候执行方法postProcessBeanFactory()方法,
// 而BeanDefinitionRegistryPostProcessor是在BeanFactoryPostProcessor的前面执行,在源码
// org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors()方法里面定义了执行顺序
// BeanFactoryPostProcessor是bean工厂的bean属性处理容器,说通俗一些就是可以管理我们的bean工厂内所有的beandefinition(未实例化)数据,可以随心所欲的修改属性。
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" =》对应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
  // 调用我们的bean工厂的后置处理器.加载bean定义(不是实例化),通过ConfigurationClassPostProcessor去加载启动类中的扫描路径
  // 然后将路径下到bean加载进来
  invokeBeanFactoryPostProcessors(beanFactory);
​
  registerBeanPostProcessors(beanFactory);
​
  initMessageSource();
​
  initApplicationEventMulticaster();
​
  // 这个方法同样也是留个子类实现的springboot也是从这个方法进行启动tomat的.
  onRefresh();
​
  registerListeners();
​
  //实例化我们剩余的单实例bean.
  finishBeanFactoryInitialization(beanFactory);
​
  // 最后容器刷新 发布刷新事件(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();
     }
 }
    }

到此这篇关于传统tomcat启动服务与springboot启动内置tomcat服务的区别的文章就介绍到这了,更多相关tomcat启动服务与springboot启动内置tomcat服务区别内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/304993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号