遇到org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat问题原因很多,网上说的一般情况不一样。
protectedvoidonRefresh(){super.onRefresh();try{// 很明显就是这个方法createWebServer();}catch(Throwable ex){thrownewApplicationContextException("Unable to start web server", ex);}}
privatevoidinitialize()throws WebServerException {
TomcatWebServer.logger
.info("Tomcat initialized with port(s): "+getPortsDescription(false));synchronized(this.monitor){try{addInstanceIdToEngineName();
Context context =findContext();
context.addLifecycleListener((event)->{if(context.equals(event.getSource())&& Lifecycle.START_EVENT.equals(event.getType())){// Remove service connectors so that protocol binding doesn't// happen when the service is started.removeServiceConnectors();}});// Start the server to trigger initialization listenersthis.tomcat.start();// We can re-throw failure exception directly in the main thread// 重点是这个,按照我的理解这个方法会收集启动时的异常并抛出rethrowDeferredStartupExceptions();try{
ContextBindings.bindClassLoader(context, context.getNamingToken(),getClass().getClassLoader());}catch(NamingException ex){// Naming is not enabled. Continue}// Unlike Jetty, all Tomcat threads are daemon threads. We create a// blocking non-daemon to stop immediate shutdownstartDaemonAwaitThread();}catch(Exception ex){stopSilently();thrownewWebServerException("Unable to start embedded Tomcat", ex);}}}