Springboot項目全部依賴注解的,web工程是如何啟動的
1 首先引入了Tomcat依賴,然后用java代碼啟動Tomcat容器,默認Tomcat版本是8.5版本
2 Tomcat是實現了servlet3.0規范,在servlet加載類的過程中會尋找實現了ServletContainerInitializer接口的類,spring為我們實現了這個接口的類是SpringServletContainerInitializer,最后又會去調用實現了WebApplicationInitializer接口的類,最后真正是實現WebApplicationInitializer接口的是這個AbstractAnnotationConfigDispatcherServletInitializer,然后在該類中去初始化spring容器和springmvc容器和servlet攔截的url
public class AppTomcat { public static void main(String[] args) throws ServletException, LifecycleException { start(); } public static void start() throws ServletException, LifecycleException { //創建Tomcat容器和設置端口 Tomcat tomcatServer = new Tomcat(); tomcatServer.setPort(9090); StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").getAbsolutePath()); ctx.setReloadable(false); WebResourceRoot resources = new StandardRoot(ctx); File additionWebInfClasses = new File("target/classes"); //告訴Tomcat項目的class目錄,根目錄 resources.addPreResources( new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); //啟動容器 tomcatServer.start(); tomcatServer.getServer().await(); } }
項目結構:
github下載地址:https://github.com/jake1263/MySpringBoot