preface

We must be wondering how the banner below prints out when we start SpringBoot.

PS: Posts are updated on Github–Java-Notes. If some images are not visible, you can view them directly from this page

Source code analysis

The project structure

The entrance

private Banner printBanner(ConfigurableEnvironment environment) {
  // Check whether the banner printing mode is disabled
  if (this.bannerMode == Banner.Mode.OFF) {
      return null;
   }
   // Initialize the resource loader
   ResourceLoader resourceLoader = (this.resourceLoader ! =null)?this.resourceLoader : new DefaultResourceLoader(getClassLoader());
  // Initialize the banner print object
  SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
         resourceLoader, this.banner);
  // Print banner in log
   if (this.bannerMode == Mode.LOG) {
      return bannerPrinter.print(environment, this.mainApplicationClass, logger);
   }
  // Print the banner on the console
   return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
Copy the code
// There are only three modes, close console and log files
enum Mode {

   /** * Disable printing of the banner. */
   OFF,

   /** * Print the banner to System.out. */
   CONSOLE,

   /** * Print the banner to the log file. */
   LOG

}
Copy the code
public Banner print(Environment environment, Class
        sourceClass, PrintStream out) {
  // Get the banner
  Banner banner = getBanner(environment);
  / / print the banner
   banner.printBanner(environment, sourceClass, out);
   return new PrintedBanner(banner, sourceClass);
}
Copy the code
private Banner getBanner(Environment environment) {
  // Create a clearly Banners object that inherits from the Banner interface. The content is simple: save Banner information, add a Banner method, print a Banner method
   Banners banners = new Banners();
  // Add the banner
   banners.addIfNotNull(getImageBanner(environment));
   banners.addIfNotNull(getTextBanner(environment));
   if (banners.hasAtLeastOneBanner()) {
      return banners;
   }
   if (this.fallbackBanner ! =null) {
      return this.fallbackBanner;
   }
  // If none is present, the default banner will be printed, as we have all seen
   return DEFAULT_BANNER;
}
Copy the code
// Get image banner information
private Banner getImageBanner(Environment environment) {
  	// Get the banner location information in the configuration file
		String location = environment.getProperty(BANNER_IMAGE_LOCATION_PROPERTY);
		if (StringUtils.hasLength(location)) {
			Resource resource = this.resourceLoader.getResource(location);
			return resource.exists() ? new ImageBanner(resource) : null;
		}
  	// If it is not specified in the configuration file, we read the default configuration, starting with the banner prefix, traversing the matches one by one. According to the GIF. JPG, PNG order, so if it matches the previous image, the subsequent image is not displayed
		for (String ext : IMAGE_EXTENSION) {
			Resource resource = this.resourceLoader.getResource("banner." + ext);
			if (resource.exists()) {
				return newImageBanner(resource); }}return null;
	}
Copy the code
// The default banner
class SpringBootBanner implements Banner {

   private static final String[] BANNER = { "".____ ____ _"."/\\\\ / ___'_ ___ _(_)_ __ ___ \\\\ \\\\".\ \ "(() ___ | '_ |' _ | | '_ \ \ / _ ` | \ \ \ \ \ \ \ \"."\ \ \ \ / ___) | | _) | | | | | | | (_ | |))))".There comes "| |. __ _ - | | | _ _ - | | | _ \ \ __, | / / / /"."= = = = = = = = = | _ | = = = = = = = = = = = = = = | ___ / = / _ / _ / _ /" };

   private static final String SPRING_BOOT = " :: Spring Boot :: ";

   private static final int STRAP_LINE_SIZE = 42;

   @Override
   public void printBanner(Environment environment, Class
        sourceClass, PrintStream printStream) {
      for(String line : BANNER) { printStream.println(line); } String version = SpringBootVersion.getVersion(); version = (version ! =null)?" (v" + version + ")" : "";
      StringBuilder padding = new StringBuilder();
      while (padding.length() < STRAP_LINE_SIZE
            - (version.length() + SPRING_BOOT.length())) {
         padding.append(""); } printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT, AnsiColor.DEFAULT, padding.toString(), AnsiStyle.FAINT, version)); printStream.println(); }}Copy the code

The experiment

We prepared four files, GIF, JPG, PNG, TXT. The print looks like this, with the GIF content on top and the TXT content on the bottom

If we specify the file in the configuration file, it will not be displayed as the default