Skip to content

最近在做一个开源的SpringBoot学习项目,想在项目启动时加一下自己的Logo。

image-20221026210437897

启动一个SpringBoot项目就可以看到控制台输出了这个Spring的logo,我们想把她改成自己的Logo也很简单。

首先在项目resource文件夹下创建banner.txt文件,然后将我们需要的替换彩蛋的内容输入到这个文件里。 这样,再次启动项目时,彩蛋就已经被替换了。

img

推荐几个字符/图片转化的网站
http://patorjk.com/software/taag/
http://www.network-science.de/
https://spring-boot-banner-gen.cfapps.io/
http://www.degraeve.com/img2txt.php

在banner中还可以添加一些应用信息

${application.version} 与MANIFEST.MF文件中相同的版本号,比如1.5.4.RELEASE<br/>

${application.formatted-version} 格式化过的版本号就是加个v然后用括号包起来,比如(v1.5.4.RELEASE)<br>
${application.title} 这玩意儿在哪设置的我忘记了<br/> 
${spring-boot.version} Spring Boot的版本<br> 
${spring-boot.formatted-version} 格式化过的版本

改进之后,我们的banner内容如下

img

显示效果如下

img

根据手册描述,还可以根据以下来配置banner的颜色 背景图片 banner.tx路径,有兴趣的可以自己尝试一下

spring.banner.location=classpath:banner.txt # Banner text resource location. spring.banner.image.location=classpath:banner.gif # Banner image file location (jpg or png can also be used). spring.banner.image.width=76 # Width of the banner image in chars. spring.banner.image.height= # Height of the banner image in chars (default based on image height). spring.banner.image.margin=2 # Left hand image margin in chars. spring.banner.image.invert=false # Whether images should be inverted for dark terminal themes.

3.1. 简单方法修改

最简单的方法就是在启动方法后面直接sout打印,但是这样好像很low,所以不再演示。

image-20221026210522562

3.2. 继承初始化方法

代码

java
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author baldwin
 */
@Component
@Order(1)
public class MyStartLog implements CommandLineRunner {
   

    @Override
    public void run(String... args) throws Exception {
   
        StringBuilder commandLog = new StringBuilder();
        commandLog.append("+==================================================================================+\n");
        commandLog.append("+                        项目启动成功!!!                                             +\n");
        commandLog.append("+        我的博客地址:https://yzstu.blog.csdn.net                                    +\n");
        commandLog.append("+        当前项目地址:https://gitee.com/dikeywork/learn-springboot/                  +\n");
        commandLog.append("+        系列文章地址:https://blog.csdn.net/shouchenchuan5253/category_10223260.html +\n");
        commandLog.append("+==================================================================================+\n");
        System.out.println(commandLog.toString());
    }
}

效果 img

3.3. 拓展

在MyStartLog上的注解@Order(1)的作用是,当存在多个类似的自定义类时,用作排序

代码地址: https://gitee.com/dikeywork/learn-springboot/

更多文章地址: https://yzstu.blog.csdn.net