springboot启动流程
手动撸springboot的Tomcat启动
Servlet容器初始化类
public class MyServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
System.out.println("==============onStartup========================");
}
}
测试主类
**
* 手动启动tomcat
*
* @author zhouzx
*/
public class MyOwnTomcatApplication {
public static void main(String[] args) throws InterruptedException, LifecycleException {
Tomcat tomcat = new Tomcat();
File baseDir = new File("c:/tmp/tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(9090);
tomcat.getService().addConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
StandardContext context = new StandardContext();
context.setName("/aa");
context.setDisplayName("/aa");
context.setPath("/aa");
File docBase = new File("c:/tmp/tomcat-docbase");
context.setDocBase(docBase.getAbsolutePath());
context.addLifecycleListener(new Tomcat.FixContextListener());
context.setUseRelativeRedirects(false);
context.addServletContainerInitializer(new MyServletContainerInitializer(), Collections.emptySet());
WebappLoader loader = new WebappLoader(context.getParentClassLoader());
loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
loader.setDelegate(true);
context.setLoader(loader);
tomcat.getHost().addChild(context);
TomcatWebServer tomcatWebServer = new TomcatWebServer(tomcat, true);
//模拟出错,程序一直运行,未关闭,TomcatWebServer的构造方法中有Tomcat的start方法
int i = 1 / 0;
}
}