传统tomcat启动服务与springboot启动内置tomcat服务的区别

本文探讨了在spring整合springmvc时,tomcat如何通过web.xml启动并加载spring容器,以及bean的注册过程。而在springboot中,启动方式不同,它在SpringApplication.run方法中初始化上下文,然后通过refreshContext方法加载bean。springboot使用ConfigurationClassPostProcessor对bean定义进行处理,实现了更灵活的初始化机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring整合springmvc

  • spring整合springmvc中web.xml配置如下,tomcat在启动过程中会加载web.xml中的内容,ContextLoaderListener实现了tomcat里面的ServletContextListener接口,所以在tomcat容器启动过程通过ContextLoaderListener来进行spring容器的初始化操作,并将
    classpath:spring/applicationContext-*.xml指定下的spring配置文件加载,该配置文件我只配置了<context:component-scan base-package=“org.com.yp”/>,代表通过扫描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定义。
<?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>
  </li
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值