从SpringApplication run方法开始
public ConfigurableApplicationContext run ( String. . . args) {
、、、
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments ( args) ;
ConfigurableEnvironment environment = prepareEnvironment ( listeners, applicationArguments) ;
configureIgnoreBeanInfo ( environment) ;
Banner printedBanner = printBanner ( environment) ;
context = createApplicationContext ( ) ;
exceptionReporters = getSpringFactoriesInstances ( SpringBootExceptionReporter. class ,
new Class [ ] { ConfigurableApplicationContext. class } , context) ;
prepareContext ( context, environment, listeners, applicationArguments, printedBanner) ;
refreshContext ( context) ;
afterRefresh ( context, applicationArguments) ;
stopWatch. stop ( ) ;
if ( this . logStartupInfo) {
new StartupInfoLogger ( this . mainApplicationClass) . logStarted ( getApplicationLog ( ) , stopWatch) ;
}
listeners. started ( context) ;
callRunners ( context, applicationArguments) ;
}
catch ( Throwable ex) {
handleRunFailure ( context, ex, exceptionReporters, listeners) ;
throw new IllegalStateException ( ex) ;
}
、、、
}
查看 SpringApplication refreshContext 方法
private void refreshContext ( ConfigurableApplicationContext context) {
refresh ( context) ;
if ( this . registerShutdownHook) {
try {
context. registerShutdownHook ( ) ;
}
catch ( AccessControlException ex) {
}
}
}
protected void refresh ( ApplicationContext applicationContext) {
Assert. isInstanceOf ( AbstractApplicationContext. class , applicationContext) ;
( ( AbstractApplicationContext) applicationContext) . refresh ( ) ;
}
ServletWebServerApplicationContext 的refresh 方法
public final void refresh ( ) throws BeansException, IllegalStateException {
try {
super . refresh ( ) ;
}
catch ( RuntimeException ex) {
stopAndReleaseWebServer ( ) ;
throw ex;
}
}
@Override
protected void onRefresh ( ) {
super . onRefresh ( ) ;
try {
createWebServer ( ) ;
}
catch ( Throwable ex) {
throw new ApplicationContextException ( "Unable to start web server" , ex) ;
}
}
创建tomcat 容器
private void createWebServer ( ) {
WebServer webServer = this . webServer;
ServletContext servletContext = getServletContext ( ) ;
if ( webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory ( ) ;
this . webServer = factory. getWebServer ( getSelfInitializer ( ) ) ;
}
else if ( servletContext != null) {
try {
getSelfInitializer ( ) . onStartup ( servletContext) ;
}
catch ( ServletException ex) {
throw new ApplicationContextException ( "Cannot initialize servlet context" , ex) ;
}
}
initPropertySources ( ) ;
}
创建tomcat 服务器的过程
public WebServer getWebServer ( ServletContextInitializer. . . initializers) {
if ( this . disableMBeanRegistry) {
Registry. disableRegistry ( ) ;
}
Tomcat tomcat = new Tomcat ( ) ;
File baseDir = ( this . baseDirectory != null) ? this . baseDirectory : createTempDir ( "tomcat" ) ;
tomcat. setBaseDir ( baseDir. getAbsolutePath ( ) ) ;
Connector connector = new Connector ( this . protocol) ;
connector. setThrowOnFailure ( true ) ;
tomcat. getService ( ) . addConnector ( connector) ;
customizeConnector ( connector) ;
tomcat. setConnector ( connector) ;
tomcat. getHost ( ) . setAutoDeploy ( false ) ;
configureEngine ( tomcat. getEngine ( ) ) ;
for ( Connector additionalConnector : this . additionalTomcatConnectors) {
tomcat. getService ( ) . addConnector ( additionalConnector) ;
}
prepareContext ( tomcat. getHost ( ) , initializers) ;
return getTomcatWebServer ( tomcat) ;
}
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#finishRefresh
启动tomcat 服务器
protected void finishRefresh( ) {
super.finishRefresh( ) ;
// 启动web服务器
WebServer webServer = startWebServer( ) ;
if ( webServer != null) {
publishEvent( new ServletWebServerInitializedEvent( webServer, this)) ;
}
}
private WebServer startWebServer( ) {
WebServer webServer = this.webServer;
if ( webServer != null) {
// 启动
webServer.start( ) ;
}
return webServer;
}
public void start( ) throws WebServerException {
synchronized ( this.monitor) {
if ( this.started) {
return ;
}
try {
// 启动connector ,及connector使用的线程池
addPreviouslyRemovedConnectors( ) ;
Connector connector = this.tomcat.getConnector( ) ;
if ( connector != null && this.autoStart) {
// 初始化servlet, 例如DispatcherServlet
performDeferredLoadOnStartup( ) ;
}
checkThatConnectorsHaveStarted( ) ;
this.started = true ;
logger.info( "Tomcat started on port(s): " + getPortsDescription( true) + " with context path '"
+ getContextPath( ) + "'" ) ;
}
catch ( ConnectorStartFailedException ex) {
stopSilently( ) ;
throw ex;
}
catch ( Exception ex) {
if ( findBindException( ex) != null) {
throw new PortInUseException( this.tomcat.getConnector( ) .getPort( )) ;
}
throw new WebServerException( "Unable to start embedded Tomcat server" , ex) ;
}
finally {
Context context = findContext( ) ;
ContextBindings.unbindClassLoader( context, context.getNamingToken( ) , getClass( ) .getClassLoader( )) ;
}
}
}