springboot中banner的使用

最近的项目中大部分用到了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}
放上比较流行的banner,其中${}可引用配置文件中定义好的属性。












     

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值