最近的项目中大部分用到了springboot,为了更好的理解,接下来的一段时间将深入研究的springboot的源码。
一、springboot介绍
springboot作为一种全新的编程规范,它的产生简化了框架的使用,所谓简化是指简化了spring众多框架中所需的大量且繁琐的配置文件,所以springboot是一个服务于框架的框架,服务范围是简化配置文件。
关于springboot的优缺点,不再赘述。接下来我们通过跟踪日志的方式解析springboot的启动过程。
二、springboot启动banner
springboot的默认banner如下:
在启动时会检查根配置目录下有无banner.txt文件,源码如下:
1:SpringApplication.run()
public ConfigurableApplicationContext run(String... args) {
//略
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
Banner printedBanner = this.printBanner(environment); //此处设置banner
//略
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, listeners, (FailureAnalyzers)analyzers, var9);
throw new IllegalStateException(var9);
}
}
2.SpringApplication.printBanner()
private Banner printBanner(ConfigurableEnvironment environment) {
if (this.bannerMode == Mode.OFF) {
return null;
} else if (this.printBannerViaDeprecatedMethod(environment)) {
return null;
} else {
ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader : new DefaultResourceLoader(this.getClassLoader());
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter((ResourceLoader)resourceLoader, this.banner);
return this.bannerMode == Mode.LOG ? bannerPrinter.print(environment, this.mainApplicationClass, logger) : bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
}
3.SpringApplicationBannerPrinter.print()
public Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
Banner banner = this.getBanner(environment, this.fallbackBanner);
banner.printBanner(environment, sourceClass, out);
return new SpringApplicationBannerPrinter.PrintedBanner(banner, sourceClass);
}
4.SpringApplicationBannerPrinter.getBanner()
private Banner getBanner(Environment environment, Banner definedBanner) {
SpringApplicationBannerPrinter.Banners banners = new SpringApplicationBannerPrinter.Banners();
banners.addIfNotNull(this.getImageBanner(environment));
banners.addIfNotNull(this.getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
} else {
return this.fallbackBanner != null ? this.fallbackBanner : DEFAULT_BANNER;
}
}
注:自定义banner:有多种方式:
1.可自行在application.yml或者application.properties中注入banner.location并将自定义图形文件(banner.txt)放入该目录下
2. 指定banner.image.location并将图形文件(banner.jpg/banner.png/banner.gif)放入改目录下。
3.在根配置文件目录下直接放置banner.txt文件。
4.在springboot启动类中设置:
@SpringBootApplication
@ServletComponentScan
@ImportResource({ "classpath:spring-context.xml"})
public class EecMain {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication();
springApplication.setBannerMode(Banner.Mode.OFF);//此处只是设置了开关 Mode为枚举类型,可选CONSOLE,OFF,LOG
springApplication.run(EecMain.class, args);
}
}
5.application.properties或者application.yml中配置
接下来我们对自定义banner进行一些设置:
${AnsiColor.BRIGHT_RED}
:设置控制台中输出内容的颜色${application.version}
:用来获取MANIFEST.MF
文件中的版本号${application.formatted-version}
:格式化后的${application.version}
版本信息${spring-boot.version}
:Spring Boot的版本号${spring-boot.formatted-version}
:格式化后的${spring-boot.version}
版本信息
|
${AnsiColor.BRIGHT_GREEN}
_ooOoo_o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 佛祖保佑 永无BUG
${AnsiColor.BRIGHT_RED}
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
|