简易版的SpringBoot是如何实现的!!!( 二 )


创建Tomcat对象与DispatcherServlet并绑定启动public static void startTomcat(WebApplicationContext applicationContext){Tomcat tomcat = new Tomcat();Server server = tomcat.getServer();Service service = server.findService("Tomcat");Connector connector = new Connector();connector.setPort(8081);Engine engine = new StandardEngine();engine.setDefaultHost("localhost");Host host = new StandardHost();host.setName("localhost");String contextPath = "";Context context = new StandardContext();context.setPath(contextPath);context.addLifecycleListener(new Tomcat.FixContextListener());host.addChild(context);engine.addChild(host);service.setContainer(engine);service.addConnector(connector);tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(applicationContext));context.addServletMappingDecoded("/*", "dispatcher");try {tomcat.start();} catch (LifecycleException e) {e.printStackTrace();}}startTomcat方法就是启动Tomcat,需要传递一个容器,然后绑定8081端口,在浏览器中我们就可以通过“localhost:8081/test”来访问 。

简易版的SpringBoot是如何实现的!!!

文章插图
图片
 
 总结开篇简单模拟一下SpringBoot的过程,后期逐步来分析一下SpringBoot中的相关源码 。
强调一点:其中大量运用Spring的相关知识 , 如果有不理解的地方可以提出来或者去翻阅前面的知识点 。

【简易版的SpringBoot是如何实现的!!!】


推荐阅读