Spring Boot -- 启动彩蛋
使用 Spring Boot 启动的 jar 包总是会显示一个 Spring 的图标:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ))) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
实际上 Spring Boot 在这个位置,放了一个彩蛋,我们是可以自定义这个图标的。
我们可以在 resource 目录下面放入一个banner.txt
文件,Spring Boot 启动项目的时候就会优先启动这个文件中的内容。
这里给大家推荐两个个字符画生成的网站,我们可以利用生成的字符串放入这个banner.txt
文件:
比如我生成一个star wars
的图标:
_______.___________. ___ .______
/ | | / \ | _ \
| (----`---| |----` / ^ \ | |_) |
\ \ | | / /_\ \ | /
.----) | | | / _____ \ | |\ \----.
|_______/ |__| /__/ \__\ | _| `._____|
____ __ ____ ___ .______ .
\ \ / \ / / / \ | _ \ / |
\ / / / / ^ \ | |) | | (----<span class="hljs-punctuation">\</span> <span class="hljs-operator">/</span> <span class="hljs-operator">/</span> <span class="hljs-operator">/</span>_<span class="hljs-punctuation">\</span> <span class="hljs-punctuation">\</span> <span class="hljs-operator">|</span> <span class="hljs-operator">/</span> <span class="hljs-punctuation">\</span> <span class="hljs-punctuation">\</span> <span class="hljs-punctuation">\</span> <span class="hljs-operator">/</span><span class="hljs-punctuation">\</span> <span class="hljs-operator">/</span> <span class="hljs-operator">/</span> _____ <span class="hljs-punctuation">\</span> <span class="hljs-operator">|</span> <span class="hljs-operator">|</span><span class="hljs-punctuation">\</span> <span class="hljs-punctuation">\</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>.<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">|</span> <span class="hljs-punctuation">\</span>__<span class="hljs-operator">/</span> <span class="hljs-punctuation">\</span>__<span class="hljs-operator">/</span> <span class="hljs-operator">/</span>__<span class="hljs-operator">/</span> <span class="hljs-punctuation">\</span>__<span class="hljs-punctuation">\</span> <span class="hljs-operator">|</span> _<span class="hljs-operator">|</span>
.|____/
这样启动的时候
但是仅仅是这样看起来并不好看,还不如原来的图标好看呢。实际上 Spring Boot 为这个彩蛋提供了不少美化功能。
Spring Boot 提供了一个枚举类AnsiColor
,这个类可以控制banner.txt
中的字符颜色,而且非常容易使用。
比如我可以将字符设置成颜色:BRIGHT_YELLOW
${AnsiColor.BRIGHT_YELLOW}
_______.___________. ___ .______
/ | | / \ | _ \
| (----`---| |----` / ^ \ | |_) |
\ \ | | / /_\ \ | /
.----) | | | / _____ \ | |\ \----.
|_______/ |__| /__/ \__\ | _| `._____|
__ __ . .
\ \ / \ / / / \ | \ / |
\ / / / / ^ \ | |) | | (----\ / / /_</span>\ \ | / \ \ \ /\ / / __</span><span class="hljs-strong">__<span class="hljs-emphasis">_ \ | |\ \----.----) | \<span class="hljs-strong">__/ \__</span>/ /<span class="hljs-strong">__/ \__</span>\ | _</span>|
.|/
再重新启动项目,启动界面就会变成这个样子:
类似 ${AnsiColor.BRIGHT_YELLOW}
这种表达式,其实可以放置多个,启动界面上的颜色,总是会根据AnsiColor
的设置改变界面的颜色,这样启动的界面就会显示多种不同的颜色了。
除了这样美化的功能之外,启动界面很重要的功能就是要告诉我们这个项目的一些重要信息。
${application.version} 这个是MANIFEST.MF文件中的版本号
${application.formatted-version} 这个是上面的的版本号前面加v后上括号
${spring-boot.version} 这个是springboot的版本号
${spring-boot.formatted-version}同上
把以上信息通过${}
放入 banner.txt 中,就会打印出项目对应的信息。
这里放一个完整的 demo:
${AnsiColor.BRIGHT_YELLOW}
_______.___________. ___ .______ ____ __ ____ ___ .______ _______.
/ | | / \ | _ \ \ \ / \ / / / \ | _ \ / |
| (----`---| |----` / ^ \ | |_) | \ \/ \/ / / ^ \ | |_) | | (----`
\ \ | | / /_\ \ | / \ / / /_\ \ | / \ \
.----) | | | / _____ \ | |\ \----. \ /\ / / _____ \ | |\ \----.----) |
|_______/ |__| /__/ \__\ | _| `._____| \__/ \__/ /__/ \__\ | _| `._____|_______/
${AnsiColor.BRIGHT_BLUE}
::: Project (version:${application.version}) ::: \(^O^)/ Spring-Boot ${spring-boot.version}
启动就会是这一个样子: