目录
- 前言
- 1. banner.text
- 1.1 配置文件关闭
- 1.2 启动类关闭
- 1.3 命令行关闭
- 2. 自定义Banner类
- 3. 自动配置类
- 4. 总结
前言
实现自定义启动动画是一项有趣的任务,虽然Spring Boot本身不提供内置的动画功能,但可以通过一些技巧来实现
以下主要以Demo的形式展示,再者下面的Demo都可以联合使用
1. banner.text
在资源文件夹(通常是src/main/resources)下放置一个名为banner.txt的文件,来自定义应用启动时的Banner,文件的内容会在应用启动时显示在控制台中
优点如下:简单且易于实现,且只需在banner.txt文件中定义文本即可,不需要编写Java代码
缺点如下:只能显示静态文本或图案,不支持动态动画效果
整体的Demo如下:
.__ __. ______ .______ __ __ _______
| \ | | / __ \ | _ \ | | | | / _____|
| \| | | | | | | |_) | | | | | | | __
| . ` | | | | | | _ < | | | | | | |_ |
| |\ | | `--' | | |_) | | `--' | | |__| |
|__| \__| \______/ |______/ \______/ \______|
之后启动应用程序即可
截图如下:
已经设置了banner.txt文件来显示自定义启动Banner,但希望在某些情况下关闭这个Banner
1.1 配置文件关闭
在Spring Boot的配置文件(例如application.properties或application.yml)中设置spring.main.banner-mode属性为off来关闭Banner
application.properties:
spring.main.banner-mode=off
application.yml:
yaml
复制代码
spring:main:banner-mode: off
1.2 启动类关闭
只需要设置为off即可
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class CustomBannerApplication {public static void main(String[] args) {SpringApplication app = new SpringApplication(CustomBannerApplication.class);app.setBannerMode(Banner.Mode.OFF);app.run(args);}
}
1.3 命令行关闭
java -jar your-application.jar --spring.main.banner-mode=off
2. 自定义Banner类
创建一个实现Banner接口的类,例如CustomBanner:
Demo如下:
import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
import java.io.PrintStream;public class CustomBanner implements Banner {@Overridepublic void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {out.println("Custom Startup Animation");for (int i = 0; i < 10; i++) {try {out.print(".");Thread.sleep(500); // 延时0.5秒} catch (InterruptedException e) {Thread.currentThread().interrupt();}}out.println();}
}
在启动类中设置自定义Banner
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class CustomBannerApplication {public static void main(String[] args) {SpringApplication app = new SpringApplication(CustomBannerApplication.class);app.setBanner(new CustomBanner());app.run(args);}
}
相对复杂的动画特效如下:
import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;import java.io.PrintStream;public class CustomBanner implements Banner {@Overridepublic void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {out.println("Starting Application...");int max = 50;for (int i = 0; i <= max; i++) {out.print("\r[");for (int j = 0; j < max; j++) {if (j < i) {out.print("=");} else {out.print(" ");}}out.print("] " + i * 2 + "%");try {Thread.sleep(100); // 延时0.1秒} catch (InterruptedException e) {Thread.currentThread().interrupt();}}out.println("\nApplication Started.");}
}
3. 自动配置类
也可通过自动配置类
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;@AutoConfiguration
public class BannerAutoConfiguration {@Beanpublic BannerApplicationRunner bannerApplicationRunner() {return new BannerApplicationRunner();}
}
实现一个BannerApplicationRunner类,包含自定义启动动画逻辑:
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;@Component
public class BannerApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("Custom Startup Animation");for (int i = 0; i < 10; i++) {System.out.print(".");Thread.sleep(500);}System.out.println();}
}
启动类无需修改
只需要添加自动加载以及扫描对应包即可:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SuppressWarnings("SpringComponentScan")
@SpringBootApplication(scanBasePackages = {"${info.base-package}.server", "${info.base-package}.module"})
public class ServerApplication {public static void main(String[] args) {SpringApplication.run(ServerApplication.class, args);}
}
总的来说:
@AutoConfiguration
:确保BannerAutoConfiguration在应用启动时被自动加载@Bean
:定义一个bean,这个bean在应用启动时被创建并注册到Spring容器中@SpringBootApplication
:触发组件扫描和自动配置
4. 总结
- banner.txt方法:适合简单的静态文本展示,易于实现,但缺乏动态效果
- 自定义Banner类方法:适合复杂的动态效果,实现灵活,但需要编写Java代码
- 自动配置类方法:结合了Spring Boot自动配置的优点,配置类自动加载,适合更复杂的配置和动态效果
如果上述的Demo多个联合使用,需要注意时间顺序
可以在如下加入延迟,类似如下:
public class BannerApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {ThreadUtil.execute(() -> {ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒,保证输出到结尾log.info("ceshi");});}
}