SpringApplication
通常,咱們用 SpringApplication
來啓動spring boot應用。如java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
複製代碼
還有其餘兩種方式:spring
自定義SpringApplication:bash
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
複製代碼
使用Builder:app
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
複製代碼
自定義文本spring-boot
在 resources
目錄下添加 banner.txt
文件:ui
Test
${AnsiColor.YELLOW}
Test Banner Text
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
複製代碼
啓動應用時,顯示以下:this
Test
Test Banner Text
Application Version:
Spring Boot Version: 2.1.3.RELEASE (v2.1.3.RELEASE)
複製代碼
自定義banner圖
:spa
在 resources
目錄下添加 banner.png
文件 啓動應用時顯示:code
@@@*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@*@&@*@@@&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@:*@@#@@@@@@@*#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@#@@&@@*@@*@:@o@@@@:@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Test
Test Banner Text
Application Version:
Spring Boot Version: 2.1.3.RELEASE (v2.1.3.RELEASE)
複製代碼
以上的 @
行,實際上是圖片 banner.png
的字符信息。orm
接下來看一下實現邏輯, 搜索 banner.txt
:
class SpringApplicationBannerPrinter {
static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";
static final String BANNER_IMAGE_LOCATION_PROPERTY = "spring.banner.image.location";
static final String DEFAULT_BANNER_LOCATION = "banner.txt";
static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };
private Banner getBanner(Environment environment) {
Banners banners = new Banners();
// 先添加圖片 banner
banners.addIfNotNull(getImageBanner(environment));
// 再添加文本信息的 banner
banners.addIfNotNull(getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
}
// 沒有在運行環境中配置 banner 信息時, A
if (this.fallbackBanner != null) {
return this.fallbackBanner;
}
// 沒有任何的 banner 信息,使用默認
return DEFAULT_BANNER;
}
private Banner getTextBanner(Environment environment) {
String location = environment.getProperty(BANNER_LOCATION_PROPERTY,
DEFAULT_BANNER_LOCATION);
Resource resource = this.resourceLoader.getResource(location);
if (resource.exists()) {
return new ResourceBanner(resource);
}
return null;
}
private Banner getImageBanner(Environment environment) {
String location = environment.getProperty(BANNER_IMAGE_LOCATION_PROPERTY);
if (StringUtils.hasLength(location)) {
Resource resource = this.resourceLoader.getResource(location);
return resource.exists() ? new ImageBanner(resource) : null;
}
for (String ext : IMAGE_EXTENSION) {
Resource resource = this.resourceLoader.getResource("banner." + ext);
if (resource.exists()) {
return new ImageBanner(resource);
}
}
return null;
}
}
複製代碼
代碼中能夠看出:默認圖片的優先級由高到底爲:gif
, jpg
, png
.
A 處的 fallbackBanner
是個啥: 搜索賦值的地方:
SpringApplicationBannerPrinter(ResourceLoader resourceLoader, Banner fallbackBanner) {
this.resourceLoader = resourceLoader;
this.fallbackBanner = fallbackBanner;
}
複製代碼
SpringApplicationBannerPrinter
的使用的位置以下:
// SpringApplication
private Banner printBanner(ConfigurableEnvironment environment) {
......
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
resourceLoader, this.banner);
......
}
public void setBanner(Banner banner) {
this.banner = banner;
}
複製代碼
所以咱們能夠對 SpringApplication
實例進行 banner
屬性的設置,使用方式:
SpringApplicationBuilder.banner()
SpringApplication.setBanner()
小結:
spring.banner.location
, spring.banner.image.location
配置的 banner
classpath
裏面的 banner.txt
或圖片內容, 圖片命名爲 banner.[ext]
, 其中 ext
的格式按照優先級高低依次是 gif
, jpg
, png
,文本和圖片能夠共存,先展現圖片,後展現文本。圖片內部展現時不能共存。