SpringBoot2.x系列教程14--SpringBoot特性之SpringApplication详解

时间:2022-12-23 14:58:12


SpringBoot系列教程14--SpringBoot特性之SpringApplication详解

作者:一一哥

从本章节开始,我们将深入详细的介绍Spring Boot,通过阅读本节你可以了解到需要使用和定制的核心特性。

一.SpringBoot特性

1.SpringApplication简介

在SpringBoot项目中,SpringApplication为我们提供了一种启动程序的快捷方式,用于从main()方法启动Spring应用。大多数情况下,我们只需要把启动任务委托给SpringApplication.run静态方法,就可以启动项目:

public static void main(String[] args){
SpringApplication.run(MySpringConfiguration.class, args);
}

当应用启动时,你应该会看到类似下面的东西:

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)

2020-03-17 11:47:42.726 INFO 12984 --- [ main] com.yyg.boot.FeatureApplication : Starting FeatureApplication on YYG39C2 with PID 12984 (F:\onlineWorks\boot-demos\demo04\target\classes started by yyg in F:\onlineWorks)
2020-03-17 11:47:42.733 INFO 12984 --- [ main] com.yyg.boot.FeatureApplication : No active profile set, falling back to default profiles: default
2020-03-17 11:47:46.019 INFO 12984 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-17 11:47:46.044 INFO 12984 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-17 11:47:46.044 INFO 12984 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-17 11:47:46.510 INFO 12984 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-17 11:47:46.510 INFO 12984 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3642 ms
2020-03-17 11:47:47.111 INFO 12984 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-17 11:47:47.568 INFO 12984 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-17 11:47:47.572 INFO 12984 --- [ main] com.yyg.boot.FeatureApplication : Started FeatureApplication in 7.698 seconds (JVM running for 8.742)

默认情况下会显示INFO级别的日志信息,包括一些相关的启动详情,比如启动应用的用户等信息。

2.启动失败

SpringBoot中为我们提供了很多的FailureAnalyzer(可以自定义)类,在SpringBoot项目启动失败时,SpringBoot就会为我们展示一个特定的错误信息,以及展示具体的解决该问题的动作思路。

例如,如果在8080端口启动一个web应用,而该端口已被占用,那你应该可以看到类似如下的内容:

APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

2020-03-17 12:03:00.376 DEBUG 11128 --- [ main] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.
context.AnnotationConfigServletWebServerApplicationContext@233c0b17, started on Tue Mar 17 12:02:57 IRKT 2020
2020-03-17 12:03:00.380 INFO 11128 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTas
kExecutor'

如果没有可用于处理该异常的失败分析器(failure analyzers),你需要展示完整的auto-configuration报告,以便更好的查看出问题的地方。这时候我们可以启用​​org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer​​的debug属性,或开启DEBUG日志级别。

例如,使用java -jar运行应用时,可以使用如下命令启用debug属性:

$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug

3.自定义Banner

之前的章节中已经详细介绍过了,本章节就不再介绍了,具体请参考:

SpringBoot系列教程10--小花样之SpringBoot配置自定义Banner