SpringBoot
SpringBoot as a very popular micro-service framework in recent years, only need a few simple dependencies, a small amount of configuration, you can use it to quickly build a lightweight micro-service, the advantage is simple, fast, simple, the disadvantage is really too single, not suitable for the project module development. If it is a single application, such as interface forwarding and project launch, SpringBoot is suitable for these scenarios. If it is a project development, it is recommended to use SpringCloud. The following is a guide to understanding SpringBoot and how to use it. Please click On SpringBoot Starter.
Understand SpringBoot
The basic annotation
- SpringBootApplication
Load class
- TypeExcludeFilter
- AutoConfigurationExcludeFilter
- EnableAutoConfiguration
- AutoConfigurationImportSelector
The Web service
- WebServer
Load order, light yellow for annotations, light green for classes.
According to the order in the figure, and follow SpringBoot source to track breakpoints, in fact, it is not difficult to find, SpringBoot load order logical meaning:
@SpringBootApplication
public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
SpringBootApplication
The SpringBootApplication annotation marked on the boot class indicates that this is a SpringBoot project, and SpringApplication.run(application.class, args); , as the boot of SpringBoot, also entered the Initializer process in the figure;
Initializer
In Initializer, the main SpringApplication class, some parameters are initialized:
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
Copy the code
Around line 92 of the class, we initialize log, initializer, listener, and configuration files for resources: application.yaml;
Scan
In TypeExcludeFilter and AutoConfigurationExcludeFilter, bring the most subjective should be basepackage scan, like for the next action to the path of the package.
The logic here is very much like Spring’s design style. AOP first defines the aspect, scans the class under the absolute package path, and gets the corresponding pointcut. 👇
Configuration
Use reflection to instantiate these classes. If you want to use inversion of control, use reflection to instantiate these classes.
In AutoConfigurationImportSelector, access to the class:
In subsequent ConfigurationClassPostProcessor this. Reader. LoadBeanDefinitions (configClasses); Instantiate;
Web
Web is a container required by SpringBoot Restful function. The Web provides external Http requests. The default WebServer for SpringBoot is Tomcat, which can be changed to Jetty or Netty in the project configuration file.
Specific source code can see TomcatWebServer.
Using SpringBoot
It is super easy to build a SpringBoot application. You can create a Zip file in Spring Initializer and download it to your local PC.
Alternatively, select Spring Initializer from new Project in idea.
Services published by SpringBoot can be invoked using the Resttemplate, which is convenient for SpringBoot. However, if you want to transfer a large number of parameters, you are advised to check SpringCloud. 😀