Spring Boot Quickstart

First, Spring Boot is a framework based on the Spring framework, which is used to simplify the development of Spring applications. Second, the slogan of Spring Boot is that convention is more than configuration. This is because there are many disadvantages of developing Web projects based on Spring and Spring MVC. Examples include too many configuration files, cumbersome deployment processes, and complex third-party integration. Spring Boot makes it easy to create and deploy production-grade applications very quickly.

Spring Boot can:

  • Quickly create standalone Spring projects and integrate with mainstream frameworks
  • Using an embedded Servlet container such as Tomcat, the reference does not need to be implemented as a War package
  • There are a lot of starters that automatically rely on and version control
  • A large number of automatic configurations and custom configurations are also available
  • Apply runtime monitoring

1.1 – Spring Boot Quickly create and deploy applications

Spring Boot quickly creates applications

You can quickly create a Spring Boot project using Spring Initializr of IDEA

Create a controller package under quickStart and add LilithController

@RestController
public class LilithController {
    
    @RequestMapping("/lilith")
    public String lilith(a){
        return "Hello Lilith!"; }}Copy the code

Run QuickstartApplication under the QickStart package

You can see that Tomcat has been restarted on port 8080 and the journey Application has started. Type localhost:8080/lilith in your browserThe browser displays the information returned in the code.

While creating projects through Spring and Spring MVC requires a lot of Spring configuration and Spring MVC configuration, Spring Boot only needs to add a spring-boot-starter-Web dependency and a Controller class. No configuration is required.

Spring Boot Enables rapid application deployment

Spring Boot can not only quickly create applications, but also quickly deploy applications

To package the application, click the package command under Lifecycle in the Maven sidebar on the right

In the terminal, go to the target directory under the project application and run the JAR package using java-jar

Java jar - spring - the boot - quickstark - 0.0.1 - the SNAPSHOT. The jarCopy the code

The application can also be successfully started

This section describes the Spring Boot project directory

The projects created using Spring Initializr mainly contain

  • XxxxApplication: main program, project startup entry
  • Resource: Saves configuration files or HTML resources in the classpath
    • Static: Saves all static resources, such as CSS, JS, images, ICONS, and so on
    • Template: Saves HTML or FTL template pages and supports template engines such as Freemarker and Thymeleaf
    • Application. properties: Spring Boot application configuration file. You can modify the default configuration
  • XxxApplicationTests: Automatically generated main application Test classes from which other Test classes can use Spring tests

Ii. Spring Boot principle analysis

2.1 – Version and dependency management

The spring-boot-starter-web dependency was selected when the project was created. The version number of the dependency was not specified. How does Spring Boot determine the version number of the dependency?

The only place in the pop.xml file that has a version number is in the parent tag, which imports a spring-boot-starter-parent as a parent dependency, The parent of spring-boot-starter-parent dependencies is spring-boot-dependencies.Spring-boot-dependencies define the version numbers of many dependencies in the properties TAB

What packages does spring-boot-starter-web depend on under the dependency TAB in the POP.xml file?

Rely on Spring MVC, Spring Boot Tomcat Starter, and a few other starters to import the components needed for the web module to function properly.

What are the Starters? Spring Boot organized the functional scenarios, and as long as there was one “Starters” introduced in the project, related dependencies would be imported.

Check out the Spring Boot official documentation for the Starters

2.2 – Automatic configuration/main program classes

The @SpringBootApplication annotation on the main program class is very important. Without this annotation, the program cannot start. The annotation table name project is a SpringBoot project. SpringBoot launches the application by running the main method of the class annotated by the @SpringBootApplication annotation

@SpringBootApplication

View the @SpringBootApplication annotation source

  • @target (elementtype.type) : indicates that the annotation is applied to a class
  • @Inherited: Indicates that the annotation is an Inherited annotation
  • @springBootConfiguration: This is a SpringBoot configuration class
    • @configuration annotation: indicates the Configuration class
    • @Component Annotation: Annotated as a Component of the application
  • @enableAutoConfiguration: Enable the automatic configuration function. Automatic configuration of Spring Boot requires this comment
    • @autoConfigurationPackage: Automatic configuration package
    • @ Import (AutoConfigurationPackages. The Registrar. The class) : @ Import annotations are Spring bottom annotation, which is used to Import components, both Import AutoConfigurationPackages. The Registrar. The class

Deubg @import Import Registrar

To set breakpoints

Right-click “New PackageImports(Metadata).getPackAgenames ()” and select Evaluate ExpressionClick on the Evaluate

The Registrar role is to register the main application class package and beans under all the subpackages into the Spring container

Deubg @ Import annotations AutoConfigurationImportSelector

Breakpoint on getCandidateConfigurations method on the left side of the play, and run Dubg modeThere are a lot of auto-configuration classes that are imported into the container, so you don’t have to manually configure them, so where are they configured?

Restart the Debug mode to AutoConfigurationImportSelector line 123, click on the Step Into; Step Into to go inside the loadFactoryNames method. Step Over; Go to line 132 of the SpringFactoriesLoader class and click Step Into to enter the loadSpringFactories method. SpringFactoriesLoader (line 136, SpringFactoriesLoader) returns a breakpoint on line 136 of SpringFactoriesLoader (line 136, SpringFactoriesLoader) Result is null), if null, the configuration class is fetched from the configuration file

Click on the FACTORIES_RESOURCE_LOCATION to check that Spring Boot’s auto-configuration classes are stored in the “meta-INF/Spring.Factories” directory

Automatic configuration classes included with Spring BootSpring Boot obtains the values specified by EnableAutoConfiguration from the “meta-INF/Spring. factories” in the classpath at startup time and imports the classes that these values indicate into the Spring container. The automatic configuration takes effect

WebMVC automatic configuration class configuration

It can be seen that the automatic configuration class of Spring MVC in Spring Boot contains the contents of the original Spring MVC XML configuration file such as DispatchServlet.

Remember, (° °) Blue ✿!