Spring Boot 横幅
最后修改于 2023 年 7 月 16 日
Spring Boot 横幅教程展示了如何在 Spring Boot 应用程序中创建横幅。
Spring Boot 是一个流行的应用程序框架,用于在 Java、Kotlin 或 Groovy 中创建企业应用程序。
Spring Boot 横幅
Spring Boot 横幅是在 Spring Boot 应用程序启动时显示的一条文本消息。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.1.1)
这是默认横幅的外观。可以关闭此横幅,也可以创建自定义横幅。
关闭横幅
可以使用配置设置或编程方式禁用横幅。
spring.main.banner-mode=off
我们可以使用 spring.main.banner-mode 属性关闭横幅。
SPRING_MAIN_BANNER-MODE=off
也可以使用 SPRING_MAIN_BANNER-MODE 环境变量禁用横幅。
new SpringApplicationBuilder(MyApplication.class)
.bannerMode(Banner.Mode.OFF)
.run(args)
...
var app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
在程序上,可以使用 SpringApplicationBuilder 或 SpringApplication 关闭横幅。
Spring Boot 自定义横幅
可以在文本文件中指定横幅。横幅的默认名称是 banner.txt。横幅文件的默认位置是 src/main/resources。
spring.banner.location=classpath:custom-banner.txt
可以使用 spring.banner.location 属性配置文本文件横幅的默认位置。
可以使用专门的网站(例如 patorjk.com/software/taag)创建自定义 ASCII 横幅。
在 banner.txt 文件中,我们可以使用以下任何占位符
| 变量 | 描述 |
|---|---|
| ${application.version} | 应用程序的版本号,在 MANIFEST.MF 中声明 |
| ${application.formatted-version} | 应用程序的版本号,在 MANIFEST.MF 中声明,并格式化以供显示 |
| ${spring-boot.version} | Spring Boot 版本 |
| ${spring-boot.formatted-version} | Spring Boot 版本,格式化以供显示 |
| ${application.title} | 应用程序的标题,在 MANIFEST.MF 中声明 |
此外,可以使用 ${AnsiColor.NAME} 或 ${AnsiBackground.NAME} 等变量对文本进行着色。
spring.output.ansi.enabled 属性设置为 always。Spring Boot 横幅示例
以下应用程序使用自定义横幅。
build.gradle
...
src
├───main
│ ├───java
│ │ └───com
│ │ └───zetcode
│ │ Application.java
│ └───resources
│ application.properties
│ banner.txt
└── test
├── java
└── resources
这是 Spring Boot 应用程序的项目结构。
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
}
这是 Gradle 构建文件。spring-boot-starter 是核心启动器,包括自动配置支持、日志记录和 YAML。应用程序被打包成一个 JAR 文件。
spring.main.log-startup-info=false spring.output.ansi.enabled=always
需要 spring.output.ansi.enabled 属性才能在 Windows 上启用颜色。
__ __ ___
/\ \ __/\ \ /\_ \
\ \ \/\ \ \ \ __ \//\ \ ___ ___ ___ ___ __
\ \ \ \ \ \ \ /'__`\ \ \ \ /'___\ / __`\ /' __` __`\ /'__`\
\ \ \_/ \_\ \/\ __/ \_\ \_ /\ \__/ /\ \L\ \/\ \/\ \/\ \ /\ __/
\ `\___x___/\ \____\ /\____\\ \____\\ \____/\ \_\ \_\ \_\\ \____\
'\/__//__/ \/____/ \/____/ \/____/ \/___/ \/_/\/_/\/_/ \/____/
Application Name: ${application.title}
Application Version: ${application.version}
${AnsiColor.RED} :: Spring Boot${spring-boot.formatted-version} :: ${AnsiColor.RED}
${AnsiColor.DEFAULT}
这是我们的文本横幅。应用程序名称和应用程序版本仅在执行最终 JAR 文件时显示;即,不是来自 IDE。我们使用 ${AnsiColor.RED} 以红色输出 Spring Boot 格式化版本。
package com.zetcode;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
// .bannerMode(Banner.Mode.OFF)
.run(args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Spring Boot console application");
}
}
Application 设置 Spring Boot 应用程序。CommandLineRunner 接口表明,当一个 bean 包含在 SpringApplication 中时,它应该运行。它可用于在 Spring Boot 中创建命令行应用程序。
$ ./gradlew bootRun
__ __ ___
/\ \ __/\ \ /\_ \
\ \ \/\ \ \ \ __ \//\ \ ___ ___ ___ ___ __
\ \ \ \ \ \ \ /'__`\ \ \ \ /'___\ / __`\ /' __` __`\ /'__`\
\ \ \_/ \_\ \/\ __/ \_\ \_ /\ \__/ /\ \L\ \/\ \/\ \/\ \ /\ __/
\ `\___x___/\ \____\ /\____\\ \____\\ \____/\ \_\ \_\ \_\\ \____\
'\/__//__/ \/____/ \/____/ \/____/ \/___/ \/_/\/_/\/_/ \/____/
Application Name:
Application Version:
:: Spring Boot (v3.1.1) ::
Spring Boot console application
我们构建并运行应用程序。
在本教程中,我们使用了一个 Spring Boot 横幅。