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
You can turn off the print banner by adding the following code to the Springboot entry main function
Springapplication.setbannermode (Banner.Mode.OFF);
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