英文原文:https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-spring-application.html GitHub:https://github.com/jijicai/Spring/tree/master/spring-boot
第四部分:Spring Boot 功能
本节将深入讨论 Spring Boot 的细节。在这里,你可以了解可能想要使用和自定义的关键功能。如果你还没有这样做,你可以能需要阅读“第二部分:入门”和“第三部分:使用 Spring Boot”,以便你有一个良好的基础知识。
23、SpringApplication
SpringApplication 类提供了一种方便的方法来引导从 main() 方法启动的 Spring 应用程序。在大多数场景下,可以委托给静态的 SpringApplication.run 方法,如下面示例所示:
public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, args); }
当你的应用程序启动时,你应该会看到类似以下输出的内容:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: v2.1.6.RELEASE 2013-07-31 00:08:16.117 INFO 56603 --- [ main] o.s.b.s.app.SampleApplication : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb) 2013-07-31 00:08:16.166 INFO 56603 --- [ main] ationConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy 2014-03-04 13:09:54.912 INFO 41370 --- [ main] .t.TomcatServletWebServerFactory : Server initialized with port: 8080 2014-03-04 13:09:56.501 INFO 41370 --- [ main] o.s.b.s.app.SampleApplication : Started SampleApplication in 2.992 seconds (JVM running for 3.658)
默认情况下,将显示 INFO 级别的日志信息,包括一些相关的启动详细信息,例如启动应用程序的用户。如果需要 INFO 级别以外的日志,可以设置它,如中第 26.4 节“日志级别”中所述。
23.1、启动失败
如果你的应用程序启动失败,注册的 FailureAnalyzers 将有机会提供专用的错误消息和修复问题的具体操作。例如,如果你在端口 8080 上启动 web 应用并且端口已在使用,则你应该会看到类似以下信息的内容:
*************************** APPLICATION FAILED TO START *************************** Description: Embedded servlet container 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.
注释:Spring Boot 提供了许多 FailureAnalyzer 实现,你可以添加自己的实现。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/howto-spring-boot-application.html#howto-failure-analyzer)
如果没有故障分析程序能够处理异常,你仍然可以显示完整的条件报告,以便更好地理解出错的原因。为此,需要为以下类启用 debug 属性或启用 DEBUG 日记记录:
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
例如,如果使用 java -jar 来运行应用,则可以按如下方式启用 debug 属性:
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
23.2、自定义 Banner
可以通过将 banner.txt 文件添加到类路径或将 spring.banner.location 属性设置为此类文件的位置来更改在启动时打印的 banner(横幅)。如果文件的编码不是 UTF-8,则可以设置 spring.banner.charset。除了文本文件,还可以将 banner.gif、banner.jpg 或 banner.png 图像文件添加到类路径中,或设置 spring.banner.image.location 属性。图像被转换成 ASCII 艺术表现形式并打印在任何文本横幅的上方。
在 banner.txt 文件中,可以使用以下任何占位符:
表 23.1:Banner 变量
变量(Variable) 描述(Description) ${application.version} 在 MANIFEST.MF 中声明的应用的版本号,例如,Implementation-Version:1.0 被打印为 1.0。 ${application.formatted-version} 应用程序的版本号,在 MANIFEST.MF 中声明并格式化以供显示(用括号括起来,前缀为 v)。例如(v1.0)。 ${spring-boot.version} 正在使用的 Spring Boot 版本。例如:2.1.6.RELEASE。 ${spring-boot.formated-version} 正在使用的 Spring Boot 版本,格式化以供显示(用括号括起来,前缀为v)。例如:2.1.6.RELEASE。 ${Ansi.NAME}(或 ${AnsiColor.NAME}、${AnsiBackground.NAME}、${AnsiStyle.NAME})其中 NAME 是 ANSI 转义代码的名称。有关详细信息,请参见 AnsiPropertySource。 ${application.title} 在 MANIFEST.MF 中声明的应用标题。例如,Implementation-Title:MyApp 打印为 MyApp。
提示:如果要以编程方式生成横幅,则可以使用 SpringApplication.setBanner(...) 方法。使用 org.springframework.boot.Banner 接口冰实现你自己的 printBanner() 方法。
你还可以使用 spring.main.banner-mode 属性来确定是否必须在 System.out(控制台)上打印横幅,发送到配置的记录器(日志)或根本不生成横幅(关闭)。
打印出来的 banner 被注册为一个单例 bean,名称如下:springBootBanner。
注释:YAML 将 off 映射为 false,因此在想禁用应用中的 banner 时确保添加引号,如下面示例所示:
spring: main: banner-mode: "off"
本文暂时没有评论,来添加一个吧(●'◡'●)