Spring Boot (vii) Extended analysis

Source: Internet
Author: User

In the previous chapters during the analysis of the Springboot boot process, we found that Springboot uses the Springfactoriesloader class provided by the spring framework to implement the retrieval meta-inf/ The spring.factories configuration file, and gets the specified configuration item (usually a specific interface implementation Class) from the configuration file, and then instantiates it into the spring container, which provides a convenient extension to springboot. Here are a few extension points that describe how to extend:

Extended Applicationcontextinitializer Interface

Applicationcontextinitializer the functionality of this interface is primarily called before the Spring application context (Configurableapplicationcontext) performs a refresh, and the description in the official document is " It is commonly used as a Web application in some program design in spring container initialization. such as registering some familiar configuration or activating some configuration files for (Configurableapplicationcontext getenvironment () method) ", extending this interface can achieve some additional processing.

The extension code is as follows, just implement the Initialize () method in the interface and add the instance to the container:

1@Order (1)2  Public classApplicationcontextinitializertestImplementsOrg.springframework.context.applicationcontextinitializer<configurableapplicationcontext> {3 @Override4      Public voidInitialize (Configurableapplicationcontext applicationcontext) {5 6System.out.println ("Here is a custom applicationcontextinitializer that can be used to manipulate ApplicationContext");7 8     }9}

Can be added to the spring container in any of the following ways:

    • Add Application.addinitializers (New Applicationcontextinitializertest ()) directly to the Springboot entry main function;
    • By adding a configuration entry in Application.properties:
Context.initializer. class=com.sl.springbootdemo.extendsion. Applicationcontextinitializertest

If more than one initialize is extended, it can be separated by commas (why this can be configured to see the classes provided by the Springboot jar package: Delegatingapplicationcontextinitializer)

    • Before parsing the springboot boot process, we know that Springboot will read the configuration item in Meta-inf/spring.factories when it starts, and then the custom initialize is also defined by the meta-inf/in the project custom Registering in the Spring.factories file
Org.springframework.context.applicationcontextinitializer= com.sl.springbootdemo.extendsion. Applicationcontextinitializertest
Extended Commandlinerunner or Applicationrunner interface

The function of the Commandlinerunner, Applicationrunner interface is the last step after the successful container launch, the analysis of the source code can know there is a callrunners () method, the two interfaces are provided by the Springboot framework, The difference between the two interfaces is only a different parameter type.

Extended Commandlinerunner:

1@Order (2)2 @Component3  Public classCommandlinerunnertestImplementsCommandlinerunner {4 @Override5      Public voidRun (String ... args)throwsException {6System.out.println ("Extended commandlinerunner, print it out to see");7     }8}

Extended Applicationrunner:

@Order (3) @Component Public classApplicationrunnertestImplementsApplicationrunner {//Applicationarguments is a further encapsulation of the parameters (Main method)@Override Public voidRun (applicationarguments args)throwsException {System.out.println ("===myapplicationrunner===" +arrays.aslist (Args.getsourceargs ())); System.out.println ("===getoptionnames========" +args.getoptionnames ()); System.out.println ("===getoptionvalues=======" +args.getoptionvalues ("name")); System.out.println ("==getoptionvalues========" +args.getoptionvalues ("password")); }}
Extended Applicationlistener

The listener extension can refer to the previous chapter: Spring Boot (vi) custom events and monitoring

Custom Banner

Banner repair that itself does not make sense, springboot when the start will print out a very ugly "SPRING" a few female characters

    • Close Banner

You can turn off the print banner by adding the following code to the Springboot entry main function

  Springapplication.setbannermode (Banner.Mode.OFF);
    • Modify Banner

Create a new Banner.txt file under the resources directory of the Springboot project, and start again will print the contents of the Banner.txt, although it is good to put some fanciful content in the text.

For example, the following is a comparison of Chinese wind:

Spring Boot (vii) Extended analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.