Spring boot Banner和icon
Posted xiaoduup
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot Banner和icon相关的知识,希望对你有一定的参考价值。
文章目录
Spring boot Banner和icon
上一节
源码
banner 和icon 就是做一些启动或者页面系统标识的作用;
首先创建一个web项目,如果不是很熟悉请参考上一节:Spring boot 基础web入门搭建 搭建
知识点:
- 自定义banner 和icon
- 初识 springboot 启动流程
Spring boot banner
springboot 自带的banner
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
自定义banner
- 在resources 目录下创建banner.txt文件
- 制作banner
banner 制作网站 - 修改banner.txt文件
$AnsiColor.BLUE
$spring-boot.version
$spring.application.name
$info.name
_ _ _
/' ) _ ( ) ( `\\
/' /' (_) _ _ _ _| | _ _ `\\ `\\
< < (`\\/')| | /'_` ) /'_`\\ /'_` |( ) ( ) > >
\\ `\\ > < | |( (_| |( (_) )( (_| || (_) | /' /'
`\\__)(_/\\_)(_)`\\__,_)`\\___/'`\\__,_)`\\___/'(_/'
$可以获取配置文件中的信息, $AnsiColor.BLUE 修改字体颜色 $AnsiBackground.YELLOW修改背景颜色
从新启动,日志打印
2.1.6.RELEASE
banner-demo
banner-icon-demo
_ _ _
/' ) _ ( ) ( `\\
/' /' (_) _ _ _ _| | _ _ `\\ `\\
< < (`\\/')| | /'_` ) /'_`\\ /'_` |( ) ( ) > >
\\ `\\ > < | |( (_| |( (_) )( (_| || (_) | /' /'
`\\__)(_/\\_)(_)`\\__,_)`\\___/'`\\__,_)`\\___/'(_/'
关闭banner
通过springapplication.setBannerMode 进行设置;banner.mode 有三种配置,off 关闭,console控制台,log 打印到日志;
public static void main(String[] args)
// ConfigurableApplicationContext run = SpringApplication.run(ApplicationStart.class, args);
SpringApplication springApplication = new SpringApplication();
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.addPrimarySources(Stream.of(ApplicationStart.class).collect(Collectors.toList()));
springApplication.run(args);
使用图片充当banner
resources 目录下创建banner.jpg, 默认支持 "gif", "jpg", "png" 这几种格式
源码分析
启动入口 springApplication.run 方法 > pringtBanner(environment)
private Banner printBanner(ConfigurableEnvironment environment)
if (this.bannerMode == Banner.Mode.OFF)
return null;
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
: new DefaultResourceLoader(getClassLoader());
// 使用SpringApplicationBannerPrinter 进行打印
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
if (this.bannerMode == Mode.LOG)
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
bannerPrinter.print
public Banner print(Environment environment, Class<?> sourceClass, Log logger)
Banner banner = getBanner(environment);
try
logger.info(createStringFromBanner(banner, environment, sourceClass));
catch (UnsupportedEncodingException ex)
logger.warn("Failed to create String for banner", ex);
return new PrintedBanner(banner, sourceClass);
getBanner, 默认使用的是 SpringBootBanner
private Banner getBanner(Environment environment)
Banners banners = new Banners();
banners.addIfNotNull(getImageBanner(environment));
banners.addIfNotNull(getTextBanner(environment));
if (banners.hasAtLeastOneBanner())
return banners;
if (this.fallbackBanner != null)
return this.fallbackBanner;
return DEFAULT_BANNER;
banner.printBanner(environment, sourceClass, out); 打印到控制台
Spring boot icon
在resources 目录下加入自己的favicon.ico 即可更改;springboot提供了这些静态资源路径都是可以的
“classpath:/META-INF/resources/”,“classpath:/resources/”, “classpath:/static/”, “classpath:/public/”
源码分析
重要类: WebMvcAutoConfiguration
这里就提现了 springboot 自动装配的特点
在WebMvcAutoConfiguration中找到 FaviconConfiguration静态内部类
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration implements ResourceLoaderAware
private final ResourceProperties resourceProperties;
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties)
this.resourceProperties = resourceProperties;
@Override
public void setResourceLoader(ResourceLoader resourceLoader)
this.resourceLoader = resourceLoader;
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping()
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
return mapping;
@Bean
public ResourceHttpRequestHandler faviconRequestHandler()
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(resolveFaviconLocations());
return requestHandler;
private List<Resource> resolveFaviconLocations()
String[] staticLocations = getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList<>(staticLocations.length + 1);
Arrays.stream(staticLocations).map(this.resourceLoader::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
这里通过了SimpleUrlHandlerMapping 进行了路径的映射
下一节
以上是关于Spring boot Banner和icon的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 2.0:Spring Boot 2.0尝鲜-动态 Banner