本节介绍Spring Boot的SpringApplication 的一些自定义功能
学习本篇博文之前建议学习之前的两篇基础文章
1. SpringApplication
SpringApplication类提供了一个方便的main()方法的方式来启动Spring应用程序的方法。在许多情况下,我们可以使用委派静态SpringApplication.run方法,如以下示例所示:
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
当我们运行这个主方法后可以看到下面的输出内容:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v2.1.2.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.ser[email protected]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级别的日志,包括程序启动的一些相关信息。如果想修改这个级别,稍后的博文更新中我们会讲到。
1.1 启动失败
如果Spring Boot 应用启动失败,比如这个最常见的错误
***************************
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.
很明显可以看出由于8080端口被占用,端口冲突引起的。
有时候如果错误信息还不够清楚,可以尝试使用Debug模式详细查看错误信息
在命令行下输入以下命令
java -jar myproject-0.0.1-SNAPSHOT.jar --debug
1.2 自定义Banner
Banner 指的是什么呢?我试了下终于知道了答案。
所谓的Banner ,中文意思是横幅的意思,其实就是程序启动时候这个地方
这一部分是可以自定义的,定义方法也很简单。
1.2.1 指定Banner 文件位置
首先在application.properties 文件中添加如下内容
# spring banner config
spring.banner.charset=UTF-8
spring.banner.location=/static/banner_config/banner_fo_zhu.txt
spring.banner.charset用于指定banner文件的字符编码,默认就是UTF-8,如果想修改可以在这里设置即可。
1.2.2 添加Banner 配置文件
新建banner_config 文件夹和*.txt 文件
我这里添加了很多banner选项可供选择,我们只看一个好了。
banner_fo_zhu.txt 内容如下:
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永无BUG 永不修改 //
////////////////////////////////////////////////////////////////////
1.2.3 运行程序
我们可以看到运行程序后这个地方已经被替换了
怎么样拉风不?让我们再换一个文艺点如何?
application.properties 修改如下:
spring.banner.location=/static/banner_config/banner_fo_zhu_poet.txt
效果如下:
除此之外我们还可以在Banner 文件中引入变量,比如版本号
spring-boot.version=2.1.2.RELEASE
spring-boot.formatted-version=v2.1.2.RELEASE
application.title=My App
banner_default_spring.txt模板配置如下
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: ${application.title} :: (${spring-boot.formatted-version})
我们可以看到这里已经改了,其他举一反三吧
除此之外还可以配置这个信息输出到控制台还是文件或者不输出
如果想关闭这个banner 打印,修改application.properties文件如下:
spring.main.banner-mode=off
可以看到banner 已经消失不见了
1.3 自定义SpringApplication
如果SpringApplication默认值不符合您的要求,也可以修改为创建本地实例并对其进行自定义。例如,要关闭Banner,可以这么干:
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
传递给的构造函数参数SpringApplication是Spring bean的配置源。在大多数情况下,MySpringConfiguration类中应该有这个注解@Configuration,但它们也可以是对XML配置或扫描的包的引用。
1.4 Fluent Builder API
如果不喜欢刚才的那种先把对象new出来再设置属性的配置方式,也可以使用建造者模式,类似这样链式调用:
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
完整代码如下
import javafx.scene.Parent;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SpringBoot2XHelloworldSampleApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(Parent.class)
.child(SpringBoot2XHelloworldSampleApplication.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
}
}
其他还有些知识点比如监听器之类配置还不太理解,所以这里暂时省略