SpringApplication及banner的配置
配置SpringApplication
如果SpringApplication无法满足要求,你可以自己创建一个局部实例,然后对其进行设置:
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
//关闭Banner打印
app.setBannerMode(Banner.Mode.OFF);
//添加监听器
app.addListeners(new MyListener());
...
app.run(args);
}
- 1
SpringApplication的相关配置将会被@Configuration注解的类,XML配置文件,以及Spring扫描的包引用。更详细的配置选项,参见SpringApplication Javadoc。
你也可以通过SpringApplicationBuilder来对SpringApplication的属性进行配置,这样的结构更有层次感。SpringApplicationBuilder为构建 SpringApplication 和 ApplicationContext 实例提供了一套便利的流式API:
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.listeners(new MyListener())
...
.run(args);
SpringApplication将会根据需要创建一个ApplicationContext,默认情况下,如果是非web应用,则会创建一个AnnotationConfigApplicationContext上下文,如果是web应用,则会创建一个AnnotationConfigEmbeddedWebApplicationContext上下文。当然,你也可以通过setWebEnvironment(boolean webEnvironment)来覆盖默认的设置。
ApplicationRunner 和 CommandLineRunner
如果你希望在SpringApplication启动之前完成某些操作,你可以通过实现ApplicationRunner或者CommandLineRunner接口来实现。这两个接口提供了一个run方法,会在SpringApplication.run(…)完成之前被调用。适用于系统初始化配置的加载,启动检查等等。
import org.springframework.boot.*
import org.springframework.stereotype.*
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
如果有多个CommandLineRunner或者ApplicationRunner的实现,并且这些都需要按照一定的顺序来执行,你可以通过实现org.springframework.core.Ordered接口或者使用org.springframework.core.annotation.Order注解来设置这些的执行顺序。
配置Banner
你可以通过添加一个banner.txt文件到你的classpath路径下,来改变在启动的时候打印的banner信息,或者通过设置banner.location属性来设置该文件的位置,通过banner.charset来设置文件的编码,你也可以添加banner.gif,banner.jpg, `banner.png图片文件到classpath,或者通过设置banner.image.location属性来作为banner信息,这些图片会被转换为有艺术感的ASCII,并且打印在文本的顶部。banner.txt中可以设置如下的占位符:
| 变量 | 描述 |
|---|---|
${application.version} |
The version number of your application as declared in MANIFEST.MF. For example Implementation-Version: 1.0 is printed as 1.0. |
${application.formatted-version} |
The version number of your application as declared in MANIFEST.MF formatted for display (surrounded with brackets and prefixed with v). For example (v1.0). |
${spring-boot.version} |
The Spring Boot version that you are using. For example 1.4.1.RELEASE. |
${spring-boot.formatted-version} |
The Spring Boot version that you are using formatted for display (surrounded with brackets and prefixed with v). For example (v1.4.1.RELEASE). |
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) |
Where NAME is the name of an ANSI escape code. See AnsiPropertySource for details. |
${application.title} |
The title of your application as declared in MANIFEST.MF. For example Implementation-Title: MyApp is printed as MyApp. |
可以通过设置spring.main.banner-mode属性来控制输出,如下,在application.properties文件中添加如下属性,将会覆盖SpringApplication中的默认配置:
# 打印到控制台
spring.main.banner-mode=console
# 打印到日志文件
# spring.main.banner-mode=log
# 不打印
# spring.main.banner-mode=off
SpringApplication及banner的配置的更多相关文章
- IntelliJ IDEA 2017版 SpringBoot的关闭自动配置和自定义Banner
一.关闭自动配置 在jar包下找下边的名字 设置关闭自动配置jar 多个的时候配置 二.自定义Banner (1)网站搜索一个图案.网址:http://patorjk.co ...
- jquery banner 轮播配置方法
1 概述 Banner可以作为网站页面的横幅广告,也可以作为游行活动时用的旗帜,还可以是报纸杂志上的大标题.Banner主要体现中心意旨,形象鲜明表达最主要的情感思想或宣传中心.在以往很多项目中主要体 ...
- SpringBoot(五):@ConfigurationProperties配置参数绑定
在springmvc或其他ssh框架中如果我们要实现一个配置参数的加载,需要使用代码实现读取properties文件等操作,或者需要使用其他属性@value(name="username&q ...
- SpringBoot(四):banner的控制
banner在springboot中是一个支持可配(banner的样式,banner的颜色,banner的内容).是否显示. 1)banner显示内容配置: 默认springboot如果在src/re ...
- SpringBoot系列——花里胡哨的banner.txt
前言 我们注意到springboot项目启动时,控制台会打印自带的banner,然后对于部分IT骚年来说,太单调太普通太一般了:所以,是时候表演真正的技术了 项目结构 我们只需要在springboot ...
- Spring Boot的SpringApplication类详解
相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...
- SpringBoot官方文档学习(一)SpringApplication
Springboot通过main方法启动,在许多情况下,委派给静态SpringApplication.run方法: public static void main(String[] args) { S ...
- 【Java】SpringBoot 佛祖保佑banner.txt
最新写代码有点多,拜拜佛祖,代码不出bug. 在springboot项目的resources文件夹下面创建一个banner.txt文件,springboot启动的时候默认会加载这个文件 ${AnsiC ...
- Spring Boot 知识清单(一)SpringApplication
爱生活,爱编码,微信搜一搜[架构技术专栏]关注这个喜欢分享的地方.本文 架构技术专栏 已收录,有各种JVM.多线程.源码视频.资料以及技术文章等你来拿. 一.概述 目前Spring Boot已经发展到 ...
随机推荐
- 为什么单线程的Redis却能支撑高并发
Redis的高并发和快速原因 redis是基于内存的,内存的读写速度非常快: 核心是基于非阻塞的IO多路复用机制: redis是单线程的,反而省去了很多上下文切换线程的时间: 为什么Redis是单线程 ...
- PanDownload/AD16/MDK5/CAD2019及2007/Dev-C++/Office2016专业版软件安装包
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 作者:struct_mooc 博客地址:https://www.cnblogs.com/stru ...
- 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例
通过该demo,可以 熟悉下 spring+cxf+maven+mybatis+mysql等常见后端技术整合 1. 在前面的 父工程 ws_parent 中 添加依赖 由于原来的项目是使用的cxf依赖 ...
- MySQL单机上多实例安装
首先安装mysql,不要启动MySQL,先配置vim /etc/my.cnf.[mysqld_multi]mysqld = /usr/bin/mysqld_safemysqladmin = /usr/ ...
- 关于overflow的学习
我在此记录一下我的学习到的东西,我自己不清楚所以要记录下来. overflow:hidden 但内元素的高度或宽度大于外元素的高度或宽度时,自动隐藏多余的部分,当然外元素设置了固定的高度或宽度. ov ...
- 第二篇.2、python基础之字符编码
一 了解字符编码的知识储备 一 计算机基础知识 二 文本编辑器存取文件的原理(nodepad++,pycharm,word) #1.打开编辑器就打开了启动了一个进程,是在内存中的,所以,用编辑器编写的 ...
- Go语言标准库之fmt.Print
Go语言fmt.Printf使用指南 本文整理了Go语言的标准输出流(fmt.Printf)在打印到屏幕时的格式化输出操作. 在这里按照占位符将被替换的变量类型划分,更方便查询和记忆. General ...
- (转)oracle使用expdp、impdp和exp、imp导入导出表及表结构
使用expdp.impdp和exp.imp时应该注重的事项: 1.exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. 2.expdp和impdp是服务端的工具程序,他们只能在 ...
- 如何卸载Win10 RS3上预装的office2016
原因分析: 由于微软在Win10 1709(RS3版本)上改变了office 2016家庭和学生版的预装方式(预装office 2016改为Windows Store应用商店的即点即用程序),无法使用 ...
- volatile关键字解决线程间内存共享变量同步的问题,让变量可以立即同步。