【Spring Boot】自定义 Banner 示例 | 自定义启动项目输出信息

在这一页,我们将提供spring boot自定义横幅(banner)的例子。

在应用程序启动时,spring boot会打印一个默认的标语。

我们可以使用类路径中的banner.txt文件改变默认横幅。

我们也可以使用spring boot Banner接口以编程方式改变默认横幅。

如果我们想显示图片作为横幅,那么把图片文件放在类路径中,命名为banner.jpgbanner.gifbanner.png

横幅文本文件和横幅图像文件也可以分别使用banner.locationbanner.image.location在应用程序属性文件中进行配置。

Spring boot提供了banner变量来打印banner的其他信息。

如果需要,我们可以完全禁用banner

我们可以使用自定义的横幅,如下所示。

Text Banner: 对于文本横幅,只需创建一个名为banner.txt的文件,并将其保存在src/main/resources位置。

Image Banner: 对于图片横幅,只需创建一个名为banner.gif的文件,并将其放在src\main\resources的位置。也可以使用其他文件扩展名,如jpgpng。控制台应该支持显示图片。

application.properties中,我们可以配置以下与横幅有关的属性。

banner.charset: 它配置了横幅编码。默认是UTF-8

banner.location: 它是banner文件的位置。默认是classpath:banner.txt

banner.image.location: 它配置了横幅图像文件的位置。默认是classpath:banner.gif。文件也可以是jpgpng

banner.image.width: 它配置了横幅图像的宽度,单位是char。默认为76

banner.image.height: 它配置了横幅图像的高度,以char为单位。默认是基于图像的高度。

banner.image.margin: 它是左边的图像边距,单位是char。默认为2。

banner.image.invert: 它配置了深色终端主题的图像是否应该被倒置。默认为false

现在在这个页面上,我们将提供如何一步步使用自定义横幅的例子。

示例工具版本

  1. Java 8
  2. Maven 3.3.9
  3. Spring Boot 1.4.3.RELEASE

Maven 文件

查找示例中使用的maven文件。

pom.xml


 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.concretepage</groupId>
	<artifactId>spring-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>spring-demo</name>
	<description>Demo project for Spring Boot</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
		<relativePath/>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
    	        <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-devtools</artifactId>
                        <optional>true</optional>
                </dependency> 
	</dependencies> 
	<build>
	   <plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	   </plugins>
	</build>
</project> 

在类路径中使用 banner.txt 创建自定义横幅

如果我们运行spring boot应用程序,那么我们得到一个默认的横幅,如下所示。

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

2017-01-24 20:59:38.097  INFO 4420 --- [  restartedMain] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 4420 (F:\arvind\PROJECT\mars\spring-boot\spring-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-demo) 

要创建自定义横幅,我们必须在spring boot应用程序的类路径中使用一个名为banner.txt的文件。我们应该注意文件名必须是banner.txt。找到我们的演示项目结构。
在这里插入图片描述
在项目中,我们已经创建了banner.txt文件,如下所示。

banner.txt

=========================
  CONCRETEPAGE
========================= 

找到主类来初始化SpringApplication

MyApplication.java

@SpringBootApplication
public class MyApplication {  
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.run(args);
    }       
}  

现在运行它,我们会看到默认的横幅不会被显示,而我们在banner.txt中使用的文本横幅将被显示如下。

=========================
  CONCRETEPAGE
=========================  

2017-01-23 18:51:38.095  INFO 432 --- [  restartedMain] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 432 (F:\arvind\PROJECT\mars\spring-boot\spring-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-demo)
2017-01-23 18:51:38.101  INFO 432 --- [  restartedMain] com.concretepage.MyApplication           : No active profile set, falling back to default profiles: default 

在应用程序属性文件中配置横幅文件

横幅文件可以在应用程序属性文件中配置。

Spring boot提供了banner.location属性,用于配置banner文件的位置。找到该属性文件。

application.properties

banner.location = banner/my-banner.txt 

如果我们使用应用程序属性文件来配置横幅文件,那么文件名可以是任何自定义名称。在我的例子中,横幅文件的名字是my-banner.txt,位于resources/banner文件夹中。

如果我们在类路径中放置了banner.txt文件,并在属性文件中配置了banner.location,那么在这种情况下,banner文件将从应用程序属性文件中获取,即由banner.location配置。

默认的横幅字符集是UTF-8,要改变字符集,请在应用程序属性文件中配置banner.charset属性。

用图片改变横幅

Spring boot可以在启动时显示图片横幅。

为此,我们必须在类路径中放置一个文件,命名为banner.jpgbanner.gifbanner.png

图片会被转换成ASCII码的形式。

如果我们想在属性文件中配置图片路径,那么spring boot提供了banner.image.location,配置方法如下。

application.properties

banner.image.location = banner/concretepage.jpg 

如果我们同时配置文本和图像横幅,那么spring boot将同时显示横幅,图像横幅将在文本横幅之上。

横幅变量

为了在启动时用banner显示额外的信息,spring boot提供了如下banner变量。

${application.version} : 从MANIFEST.MF文件中配置的Implementation-Version属性中提取我们应用程序的版本号。

${application.formatted-version} : 选择我们在MANIFEST.MF文件中配置的应用程序的版本号,该版本号将是(用括号包围并以v为前缀)。

${application.title} : 从MANIFEST.MF文件中配置的属性Implementation-Title中挑选应用程序的标题。

${spring-boot.version} : 它显示我们正在使用的spring boot版本,如1.4.3.RELEASE

${spring-boot.formatted-version} : 它显示的是我们正在使用的Spring Boot版本的显示格式(用括号包围,前缀为v),如例子(v1.4.3.RELEASE)。

${AnsiColor.NAME} : 它用于制作彩色的横幅,其中NAME是一个ANSI转义代码。从链接中找到NAME的值。

${AnsiBackground.NAME} : 它用于改变横幅的背景颜色,其中NAME是一个ANSI转义代码。从链接中找到NAME的值。

${AnsiStyle.NAME} : 它用于改变横幅的风格,其中NAME是一个ANSI转义代码。从链接中找到NAME的值。

我们需要在banner.txt文件或应用程序属性文件中由banner.location配置的banner文件中配置上述属性。

现在我们将讨论一个例子,使用banner变量找到我们的banner.txt文件。

resources/banner.txt

=========================
      CONCRETEPAGE
=========================  
Application Version : ${application.version}
Application Formatted Version : ${application.formatted-version}
Application Title : ${application.title}
Spring Boot Version : ${spring-boot.version}
Spring Boot Formatted Version : ${spring-boot.formatted-version}

找到我们的例子中使用的MANIFEST.MF文件。

resources/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: spring-demo
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: com.concretepage
Build-Jdk: 1.8.0
Implementation-Vendor: Pivotal Software, Inc. 

现在我们将测试我们的应用程序。

如果我们在IDE中使用main类运行应用程序,应用程序不会被完全部署,MANIFEST.MF文件也不会被读取,所以banner不会选择其值。

如果我们从命令提示符下使用mvn spring-boot:run命令以分解形式运行应用程序,同样MANIFEST.MF文件也不会被读取。

所以为了读取MANIFEST.MF文件,我们需要创建JAR。找到运行JAR的步骤。

1. 使用命令提示符进入项目的根目录,运行以下命令。

mvn clean package 

上述命令将创建一个可执行的JAR文件,例如

spring-demo-0.0.1-SNAPSHOT.jar 

2. 要运行JAR文件,请使用以下命令。

java -jar target/spring-demo-0.0.1-SNAPSHOT.jar 

现在我们将得到如下的输出。
在这里插入图片描述
我们将观察到,MANIFEST.MF文件中的值被显示为横幅变量。

横幅颜色

为了使横幅丰富多彩,spring boot提供了AnsiColor.NAMEAnsiBackground.NAME,其中NAME是一个ANSI转义代码。

AnsiColor.NAME可以从链接中找到,AnsiBackground.NAME可以从链接中找到。现在让我们创建一个彩色的横幅。

找到banner.txt

banner.txt

${AnsiColor.BRIGHT_BLUE} ${AnsiBackground.BRIGHT_RED} HELLOW WORLD! 

以编程方式创建自定义横幅

要以编程方式创建自定义横幅,我们需要遵循以下步骤。

1. Spring boot提供了Banner接口,使我们能够以编程方式改变banner。我们将创建一个实现Banner接口的类,并重写其方法printBanner()来配置banner

MyBanner.java

package com.concretepage;
import java.io.PrintStream;
import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
public class MyBanner implements Banner  {
	@Override
	public void printBanner(Environment arg0, Class<?> arg1, PrintStream arg2) {
		arg2.println("================================");
		arg2.println("----------Hello World!----------");
		arg2.println("================================");
	}
} 

2. 现在我们需要用SpringApplication配置我们的banner类。找到应用类。

MyApplication.java

@SpringBootApplication
public class MyApplication {  
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.setBanner(new MyBanner());
	application.run(args);
    }       
}  

3. 只有当我们在类路径中没有使用banner.txt文件,也没有在应用程序属性文件中配置banner.location属性时,printBanner()方法定义的横幅才会显示。找到应用程序的启动输出。

================================
----------Hello World!----------
================================
2017-01-24 22:53:21.386  INFO 5600 --- [  restartedMain] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 5600 (F:\arvind\PROJECT\mars\spring-boot\spring-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-demo) 

禁用横幅

要完全禁用横幅,我们可以使用应用程序的属性文件,也可以通过编程来实现。

1. 使用应用程序属性文件,我们需要配置spring.main.banner-mode属性,值为off,如下所示。

application.properties

spring.main.banner-mode = off 

2. 要以编程方式禁用横幅,我们需要在main方法中初始化SpringApplication类时调用setBannerMode()方法,并传递Banner.Mode.OFF值,如下所示。

MyApplication.java

@SpringBootApplication
public class MyApplication {  
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.setBannerMode(Banner.Mode.OFF);
	application.run(args);
    }       
} 

如果我们想在控制台打印横幅,那么就使用Banner.Mode.CONSOLE,如果我们想在日志上打印横幅,那么就使用Banner.Mode.LOGsetBannerMode()方法。

参考文献

【1】Customizing the Banner
【2】Spring Boot Custom Banner Example

源码下载

spring-boot-custom-banner-example.zip

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫巳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值