目录
Application Events and Listeners应用事件和监听器
Accessing Application Arguments(访问应用程序参数)
Using the ApplicationRunner or CommandLineRunner
SpringApplication类提供了一个方便的方式启动,由main()方法启动。很多情形你可以委托给static 的
SpringApplication.run
比如下面这样:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
当你的项目启动你将在控制台上看到如下日志
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v2.5.5
2021-02-03 10:33:25.224 INFO 17321 --- [ main]
o.s.b.d.s.s.SpringAppplicationExample : Starting SpringAppplicationExample using
Java 1.8.0_232 on mycomputer with PID 17321 (/apps/myjar.jar started by pwebb)
2021-02-03 10:33:25.226 INFO 17900 --- [ main]
o.s.b.d.s.s.SpringAppplicationExample : No active profile set, falling back to
default profiles: default
2021-02-03 10:33:26.046 INFO 17321 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080
(http)
2021-02-03 10:33:26.054 INFO 17900 --- [ main]
o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-02-03 10:33:26.055 INFO 17900 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache
Tomcat/9.0.41]
2021-02-03 10:33:26.097 INFO 17900 --- [ main]
o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded
WebApplicationContext
2021-02-03 10:33:26.097 INFO 17900 --- [ main]
w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization
completed in 821 ms
2021-02-03 10:33:26.144 INFO 17900 --- [ main]
s.tomcat.SampleTomcatApplication : ServletContext initialized
2021-02-03 10:33:26.376 INFO 17900 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with
context path ''
2021-02-03 10:33:26.384 INFO 17900 --- [ main]
o.s.b.d.s.s.SpringAppplicationExample : Started SampleTomcatApplication in 1.514
seconds (JVM running for 1.823)
默认的日志级别是INFO,包括一些相关的启动详情,比如启动程序的用户。如果你想改变日志级别,请在Log Levels更改。如果你想关掉,配置这个属性spring.main.log-startup-info 为false。
另外你也可以重写SpringApplication 子类的logStartupInfo(boolean)方法。
Startup Failure启动失败
如果你的程序启动失败,被注册的
FailureAnalyzers
获得一次提供专用的错误信息,和具体修复的动作的机会。下面这个例子,错误描述:表明8080端口已经被占用,恢复动作:定义和停止监听8080这个端口或者重新配置另外一个端口。
***************************
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.
Lazy Initialization懒加载
sprigApplication允许程序懒加载。如下配置即可实现