Using Custom Banners in Spring Boot – 在Spring Boot中使用自定义横幅

最后修改: 2017年 3月 16日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

By default, Spring Boot comes with a banner which shows up as soon as the application starts.

默认情况下,Spring Boot带有一个横幅,应用程序一启动就会显示出来。

In this article, we’ll learn how to create a custom banner and use it in Spring Boot applications.

在这篇文章中,我们将学习如何创建一个自定义横幅并在Spring Boot应用程序中使用它。

2. Creating a Banner

2.创建一个横幅

Before we start, we need to create the custom banner which will be displayed at the time of application start-up time. We can create the custom banner from scratch or use various tools that will do this for us.

在我们开始之前,我们需要创建自定义横幅,它将在应用程序启动的时候显示。我们可以从头开始创建自定义横幅,也可以使用各种工具为我们做这个。

In this example we used Baeldung’s official logo:

在这个例子中,我们使用了Baeldung的官方标志。

Screenshot-from-2017-03-15-18-29-35

However, in some situation, we might like to use the banner in the plain text format since it’s relatively easier to maintain.

然而,在某些情况下,我们可能喜欢使用纯文本格式的标语,因为它相对来说更容易维护。

The plain-text custom banner which we used in this example is available here.

我们在本例中使用的纯文本自定义横幅可以在这里获得。

Point to note here is that ANSI charset has the ability to display colorful text in the console. This can’t be done with the simple plain text format.

这里需要注意的是,ANSI字符集有能力在控制台中显示彩色的文本。这一点在简单的纯文本格式中是做不到的。

3. Using the Custom Banner

3.使用自定义标语

Since we have the custom banner ready, we need to create a file named banner.txt in the src/main/resources directory and paste the banner content into it.

由于我们已经准备好了自定义的横幅,我们需要在src/main/resources目录下创建一个名为banner.txt的文件,并将横幅内容粘贴到其中。

Point to note here is that banner.txt is the default expected banner file name, which Spring Boot uses. However, if we want to choose any other location or another name for the banner, we need to set the spring.banner.location property in the application.properties file:

这里需要注意的是,banner.txt是默认的预期banner文件名,Spring Boot使用它。但是,如果我们想为横幅选择任何其他位置或其他名称,我们需要在application.properties文件中设置spring.banner.location属性。

spring.banner.location=classpath:/path/to/banner/bannername.txt

We can also use images as banners too. Same as with banner.txt, Spring Boot expects the banner image’s name as banner.gif. Additionally, we can set different image properties such as height, width, etc. in the application.properties:

我们也可以使用图片作为横幅。与banner.txt一样,Spring Boot期望横幅图像的名称为banner.gif。此外,我们可以在application.properties中设置不同的图像属性,如高度、宽度等。

spring.banner.image.location=classpath:banner.gif
spring.banner.image.width=  //TODO
spring.banner.image.height= //TODO
spring.banner.image.margin= //TODO
spring.banner.image.invert= //TODO

However, it’s always better to use text format because the application start up time will drastically increase if some complex image structure is used.

然而,使用文本格式总是更好的,因为如果使用一些复杂的图像结构,应用程序的启动时间将急剧增加。

4. Conclusion

4.结论

In this quick article, we showed how to use a custom banner in Spring Boot applications.

在这篇快速文章中,我们展示了如何在Spring Boot应用程序中使用自定义横幅。

Like always, the full source code is available over on GitHub.

像往常一样,完整的源代码可以在GitHub上找到