一、选择合适的工具
Spring Boot Banner是一种在应用程序启动时显示的自定义图像,可以为应用程序添加专业和有趣的外观。如果你想要打造一个让人眼前一亮的Spring Boot Banner设计,需要选择一种合适的工具。
常用的工具有字符画生成器和图片转字符工具。
1.字符画生成器
字符画是通过将像素的模拟空间映射到制表符和其他字符的空间,生成的一种类似图片、图案的文字。
在Java中,我们可以使用ASCII艺术字生成器(http://www.kammerl.de/ascii/AsciiSignature.php)生成自定义的字符画,或使用Figlet(http://www.figlet.org/)生成高质量的字符画。
以下是使用ASCII艺术字生成器生成的Spring Boot Banner设计的代码片段:
System.out.println("");
System.out.println(" __ __ __ ");
System.out.println(" .-----.-----.--| |--| |.-----.--.--.----.|__|.-----.---.-.----. ");
System.out.println(" | _ | _ | _ <| || -__| | | __|| || _ | _ | __| ");
System.out.println(" |___ |___ |_____||__||_____|_____|____||__||_____|___._|____| ");
System.out.println(" |_____|_____| ");
2.图片转字符工具
图片转字符工具是将图片转换为字符画的工具。
在Java中,我们可以使用开源库:JavaFiglet(https://github.com/heikoseeberger/javafiglet)实现图片转字符。
以下是使用JavaFiglet生成的Spring Boot Banner设计的代码片段:
FigletFont customFont = FigletFont.create(new URL("http://www.jave.de/figlet/fonts/slscript.flf"));
String asciiArtBanner = FigletRenderer.render(customFont, "SPRING" + System.lineSeparator() + "BOOT");
System.out.println(asciiArtBanner);
二、自定义颜色和样式
除了使用字符画生成器或图片转字符工具外,我们还可以自定义Spring Boot Banner的颜色和样式,从而打造一个让人眼前一亮的设计。
在Spring Boot中,我们可以通过在src/main/resources下创建banner.txt或banner.*文件,并在其中添加特定字符来自定义Banner样式。
以下是一个自定义颜色和样式的代码片段:
${AnsiColor.RED}
${AnsiStyle.BOLD} __ __ __
${AnsiStyle.NORMAL} .-----.-----.--| |--| |.-----.--.--.----.|__|.-----.---.-.----.
${AnsiStyle.NORMAL} | _ | _ | _ <| || -__| | | __|| || _ | _ | __|
${AnsiStyle.NORMAL} |___ |___ |_____||__||_____|_____|____||__||_____|___._|____|
${AnsiStyle.NORMAL} |_____|_____|
${AnsiColor.DEFAULT}
三、添加动画效果
为了吸引眼球,我们可以在Banner中添加一些动画效果。Spring Boot提供了一个Spring Boot Banner图标动画的工具库:Indicators(https://github.com/ctongfei/progressbar)。
以下是应用Indicators生成的图标动画代码片段:
Spinners spinner = new Spinners(Spinners.SpinnersName.DEFAULT); System.out.println(spinner.next() + " SPRING BOOT APPLICATION " + spinner.next());
四、完整代码示例
综合以上的三个方面,我们可以在Spring Boot中创建一个自定义Banner,以打造一个让人眼前一亮的设计。
以下是一个完整的示例代码:
import com.github.javafaker.Faker;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarStyle;
import me.tongfei.progressbar.indeterminate.IndeterminateProgressBar;
import me.tongfei.progressbar.indeterminate.ProgressIndicators;
import me.tongfei.progressbar.indeterminate.style.DotsProgressBarStyle;
import me.tongfei.progressbar.indeterminate.style.LettyProgressBarStyle;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableScheduling
@EnableAsync
public class BannerDemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(BannerDemoApplication.class)
.web(WebApplicationType.SERVLET)
.banner(new CustomBanner())
.run(args);
}
@GetMapping("/")
public String home() {
return "Hello World!";
}
static class CustomBanner implements Banner {
@Lazy
@Bean
public ProgressBar progressBar() {
return new ProgressBar("Test progress", 100,
new DotsProgressBarStyle().withColorScheme(new ProgressBar.SymbloxColorScheme(
ProgressBarStyle.ASCII, ProgressBar.Color.BLUE, ProgressBar.Color.CYAN, ProgressBar.Color.GREEN)))
.start();
}
@Override
public void printBanner(ConfigurableApplicationContext configurableApplicationContext, Class aClass, java.io.PrintStream printStream) {
ProgressBar progressBar = configurableApplicationContext.getBean(ProgressBar.class);
FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-GB"), new Faker());
String name = fakeValuesService.name().fullName();
progressBar.maxHint(10);
progressBar.bar();
progressBar.stepTo(2);
progressBar.step();
progressBar.setExtraMessage("Loading Spring Boot");
progressBar.stepTo(4);
progressBar.step();
progressBar.setExtraMessage("Initializing application");
progressBar.stepTo(6);
progressBar.step();
progressBar.setExtraMessage("Setting up environment");
progressBar.stepTo(8);
progressBar.step();
progressBar.setExtraMessage("Starting server");
progressBar.stepTo(10);
progressBar.step();
printStream.println("Welcome " + name + " to Spring Boot!");
}
}
}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/285528.html
微信扫一扫
支付宝扫一扫