SpringApplication
SpringApplication类提供了一个方便的方式来引导Spring应用程序从main()方法开始。在许多情况下,您可以委派静态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会显示日志记录消息,包括一些相关的启动详细信息,例如启动应用程序的用户。
启动失败分析
如果您的应用程序无法启动,则已注册的FailureAnalyzers类有机会提供专用错误消息和具体操作来解决问题。例如,如果您在端口上启动Web应用程序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.
如果没有故障分析器能够处理异常,您仍然可以显示完整的条件报告,可以按如下方式启用该属性:
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
自定义横幅
就是修改下面这货:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v2.1.6.RELEASE
可以通过向类路径添加文件banner.txt或将spring.banner.location属性设置为此文件的位置来更改启动时打印的横幅描述文件。如果文件的编码不是UTF-8,则可以进行设置spring.banner.charset属性。除了一个文本文件,你还可以添加一个banner.gif,banner.jpg或banner.png 图像文件到类路径或设置spring.banner.image.location属性。图像将转换为ASCII艺术表示,并打印在任何文本横幅上方。
示例:

在banner.txt文件中,您可以使用以下任何占位符(必须在属性文件中配置才有值显示
):
变量 | 描述 |
---|---|
${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.formatted-version} |
您正在使用的Spring Boot版本,格式化显示(用括号括起来并带有前缀v )。例如(v2.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(console)上打印横幅,发送到配置的记录器(log),或者根本不生成横幅(off)
application.properties禁用横幅
spring.main.banner-mode=off
或者调用方法禁用横幅
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
访问应用程序参数
如果需要访问传递给应用程序的参数, SpringApplication.run(…)可以注入 org.springframework.boot.ApplicationArgumentsbean。该ApplicationArguments 接口提供对原始String[]参数以及解析option 和non-option参数的访问,如以下示例所示:
import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// 如果运行参数是"--debug logfile.txt" 则对应的变量
//debug=true, files=["logfile.txt"]
}
}
使用ApplicationRunner或CommandLineRunner
如果您需要在SpringApplication启动前运行某些特定代码,则可以实现ApplicationRunner或CommandLineRunner接口。两个接口以相同的方式工作并提供单个run方法,该方法在SpringApplication.run(…)完成之前调用 。
所述CommandLineRunner接口提供访问的应用程序的参数作为一个简单的字符串数组,而ApplicationRunner使用了ApplicationArguments前面所讨论的接口。以下示例显示了CommandLineRunner一个run方法:
import org.springframework.boot.*;
import org.springframework.stereotype.*;
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
如果定义了必须按特定顺序调用的多个CommandLineRunner
或ApplicationRunnerbean
,则可以另外实现 org.springframework.core.Ordered
接口或使用 org.springframework.core.annotation.Order
注释
退出状态码
每个都SpringApplication
注册一个与JVM相关联的关闭钩子,以确保 ApplicationContext
在退出时正常关闭。可以使用所有标准的Spring生命周期回调(例如DisposableBean
接口或@PreDestroy
注释)。
此外,如果Bean 在SpringApplication.exit()
调用时希望返回特定的退出代码,则可以实现org.springframework.boot.ExitCodeGenerator
接口。然后可以传递此退出代码以将其作为状态代码返回,如以下示例所示:
@SpringBootApplication
public class ExitCodeApplication {
@Bean
public ExitCodeGenerator exitCodeGenerator() {
return () -> 42;
}
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class, args)));
}
}
其他项
- Fluent Builder API
- 应用程序事件和监听器
- 网络环境
- 管理员功能
返回目录
网友评论