springboot configure the startup banner and custom configuration Principle Analysis

Foreword

We note springboot when the project started, the console will print that comes with banner, then for some students who compare air show, too monotonous too ordinary too general; therefore, is the real time performance of the technology

 

Custom banner

Very simple, just add a custom banner.txt file in the resource directory can

 

banner.txt content

${AnsiColor.BRIGHT_RED}
////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//             | |: `- \`; `\ _ /`;.`/ -`:. | |                  // 
//             . \ \ `- \ _ __ \ / __ _ /.-` / /                  // 
/ /       ======== `` -.____ -.___ \ _____ / ___.- ____.- ` '========          // 
//                            ` = --- ='                               // 
/ /       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^         // 
//         Namo Namo Amitabha Buddha Amitabha              // 
////////////////////////////////// //////////////////////////////////

 

================= dividing line =============================== =

As an air show program ape, of course we can not just stop at will configure a custom banner, let's go to the source code in the following Chou Chou why such configuration can achieve a custom banner, to go from ~

First, enter the Application of the main entrance of the function starts the run method, let's take a look now go step by step

SpringApplication class 

public  static ConfigurableApplicationContext RUN (<?> Class PrimarySource, String ... args) {
         return RUN ( new new Class <?> [] {} PrimarySource, args); 
    } // new new SpringApplication (primarySources) we do not have this method See, this is a logical springboot automatic assembly (SPI), and we look directly behind RUN (args )
 

public static ConfigurableApplicationContext RUN (Class <?> [] primarySources, String [] args) { return new new SpringApplication (primarySources). RUN (args) ; } public ConfigurableApplicationContext RUN (String ... args) { the StopWatch StopWatch = new new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment =prepareEnvironment (in the Listeners, applicationArguments); configureIgnoreBeanInfo (Environment);
       // we found that this is a logical step to start processing the banner, point into the look Banner printedBanner
refreshContext (context); = printBanner (Environment); context = createApplicationContext (); exceptionReporters . = getSpringFactoriesInstances (SpringBootExceptionReporter class , new new Class [] {ConfigurableApplicationContext . class }, context); prepareContext (context, Environment, the Listeners, applicationArguments, printedBanner); AfterRefresh (context, applicationArguments); stopWatch.stop (); IF ( the this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; }

processing logic banner

private Banner printBanner(ConfigurableEnvironment environment) {
        if (this.bannerMode == Banner.Mode.OFF) {
            return null;
        }
        ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
                : new DefaultResourceLoader(getClassLoader());
        SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
        if (this== .bannerMode Mode.LOG) {
             return bannerPrinter.print (Environment, the this .mainApplicationClass, Logger); 
        } 
     // Banner is not closed and is written to the log file does not specify the time, come to this
return bannerPrinter.print (Environment, the this .mainApplicationClass, the System.out); } public Banner Print (<?> Environment Environment, Class sourceClass,, PrintStream OUT) {
     // Get here banner content Banner banner
= getBanner (Environment); banner.printBanner (Environment, sourceClass,, OUT); return new new PrintedBanner (Banner, sourceClass,); } PrivateGetBanner Banner (Environment Environment) { Banners Banners = new new Banners ();
// we do not have the best air show pictures of banner content type configuration (introverted people are still a little better), so we see the next step banners.addIfNotNull (getImageBanner (environment) );
     // we are text-based configuration banner, so look getTextBanner here (Environment) banners.addIfNotNull ( getTextBanner (Environment) );
IF (banners.hasAtLeastOneBanner ()) { return Banners; } IF ( the this .fallbackBanner! = null ) { return the this .fallbackBanner; } return DEFAULT_BANNER; } // here the students should know the principles of the
Private Banner getTextBanner (Environment Environment) {
// get the custom configuration file banner address String LOCATION
= environment.getProperty (BANNER_LOCATION_PROPERTY, DEFAULT_BANNER_LOCATION ); Resource Resource = the this .resourceLoader.getResource (LOCATION); IF (resource.exists ()) { return new new ResourceBanner (Resource); } return null ; } // constant DEFAULT_BANNER_LOCATION banner.txt file points to the
static Final String = DEFAULT_BANNER_LOCATION " banner.txt ";

 

Guess you like

Origin www.cnblogs.com/vicF/p/12666675.html