延迟初始化
SpringApplication
允许延迟初始化应用程序,启用延迟初始化后,bean
将根据需要创建,而不是在应用程序启动期间创建。因此,启用延迟初始化可以减少应用程序启动所需的时间。在web
应用程序中,启用延迟初始化,将导致许多与web
相关的bean
在收到HTTP
请求之前无法初始化。
延迟初始化的一个缺点是,它会延迟应用程序问题的发现。如果一个配置错误的bean
被延迟初始化,那么在启动期间将不会发生故障,并且只有在初始化该bean
时,问题才会出现。还必须确保JVM
有足够的内存来容纳应用程序的所有bean
,而不仅仅是那些在启动期间初始化的bean
。由于这些原因,默认情况下不启用延迟初始化,建议在启用延迟初始化之前对JVM
的堆大小进行微调。
可以使用SpringApplicationBuilder
类的lazyInitialization
方法来设置延迟初始化。
package com.kaven.springboot;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringbootApplication.class)
.lazyInitialization(true)
.run(args);
}
}
或者使用SpringApplication
类的setLazyInitialization
方法进行设置。
package com.kaven.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringbootApplication.class);
application.setLazyInitialization(true);
application.run(args);
}
}
还可以在配置文件中使用spring.main.lazy-initialization
属性来设置,比如application.properties
:
spring.main.lazy-initialization=true
如果想在应用程序使用延迟初始化的同时禁用某些bean
的延迟初始化,可以使用@Lazy(false)
注解将它们的延迟属性显式设置为 false
。
自定义横幅
可以通过将banner.txt
文件添加到类路径或将spring.banner.location
属性设置为此类文件的位置来更改启动时打印的横幅。如果文件的编码不是UTF-8
,可以设置spring.banner.charset
属性。在banner.txt
文件中,可以使用以下任何占位符:
变量
| 描述
|
${spring-boot.version} | 使用的Spring Boot 版本 |
${spring-boot.formatted-version} | 使用的Spring Boot 版本,格式化显示(用括号括起来并以v 为前缀),例如(v2.6.2) |
${Ansi.NAME} 、${AnsiColor.NAME} 、${AnsiBackground.NAME} 、${AnsiStyle.NAME} | 设置控制台中输出内容的字体颜色(前两个)、背景颜色、字体格式
|
将banner.txt
文件添加到类路径。

${AnsiColor.RED}
${AnsiBackground.BLACK}
${AnsiStyle.BOLD}
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
${Ansi.YELLOW}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
启动应用,效果如下图所示。

打印方式
还可以使用spring.main.banner-mode
属性来确定横幅的打印方式,默认将横幅打印到System.out
,即输出到控制台。
enum Mode {
/**
* 禁用横幅的打印
*/
OFF,
/**
* 将横幅打印到System.out
*/
CONSOLE,
/**
* 将横幅打印到日志文件
*/
LOG
}



还可以通过SpringApplication
类的setBannerMode
方法来设置打印方式。
package com.kaven.springboot;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringbootApplication.class);
application.setBannerMode(Banner.Mode.LOG);
application.run(args);
}
}
或者通过SpringApplicationBuilder
类的bannerMode
方法来设置。
package com.kaven.springboot;
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringbootApplication.class)
.bannerMode(Banner.Mode.LOG)
.run(args);
}
}
图片文件
除了txt
文件之外,还可以将banner.gif
、banner.jpg
或banner.png
图片文件添加到类路径或设置spring.banner.image.location
属性,图片会被转换为ASCII
艺术表示并打印在横幅上方。

效果如下图所示:


效果如下图所示:

如果图片没有成功打印,看日志中有没有显示图片文件有问题,如果图片文件没有问题,使用Maven
执行clean
操作再重新启动应用。Spring Boot
的延迟初始化与自定义横幅就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。