# springboot-tomcat **Repository Path**: SueEason/springboot-tomcat ## Basic Information - **Project Name**: springboot-tomcat - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-30 - **Last Updated**: 2021-09-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 作业要求: 自定义简易版SpringBoot,实现SpringBoot MVC及内嵌Tomcat启动、DispatcherServlet注册和组件扫描功能。 程序通过main方法启动,可以自动启动tomcat服务器 可以自动创建和加载DispatcherServlet组件到ServletContext中 可以自动通过@ComponentScan扫描Controller等组件 Controller组件可以处理浏览器请求,返回响应结果 作业提示: 当实现了Servlet3.0规范的容器(比如Tomcat7及以上版本)启动时,会通过SPI扩展机制自动扫描所有jar包里META-INF/services/javax.servlet.ServletContainerInitializer文件中指定的全路径类(该类需实现ServletContainerInitializer接口),并实例化该类,并回调类中的onStartup方法。 传统SpringMVC框架web.xml的配置内容: org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-INF/app-context.xml app org.springframework.web.servlet.DispatcherServlet contextConfigLocation 1 app /app/* Spring官方文档中给出了基于Servlet3.0规范如何使用Java代码实现web.xml配置的example public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletCxt) { // Load Spring web application configuration //通过注解的方式初始化Spring的上下文 AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext(); //注册spring的配置类(替代传统项目中xml的configuration) ac.register(AppConfig.class); ac.refresh(); // Create and register the DispatcherServlet //基于java代码的方式初始化DispatcherServlet DispatcherServlet servlet = new DispatcherServlet(ac); ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet); registration.setLoadOnStartup(1); registration.addMapping("/app/*"); } } 以上知识点为作业实现基础,以此为基础,完成简易版SpringBoot,并创建Controller,能够完成方法访问。