1. Introduction to the framework

The Spring Boot framework is a framework to simplify Spring development. It is based on the reencapsulation of the Spring framework. Through automatic configuration, it simplifies the configuration required by developers to integrate different frameworks. Spring Boot is the integration of Spring technology stack, and is a one-stop solution for J2EE development.

Advantages:

  • Quickly create standalone Spring projects and integrate with mainstream frameworks;
  • With an embedded Servlet container, the application does not need to be packaged as a WAR package;
  • Automatic dependency and versioning through starter;
  • There is a lot of auto-configuration to simplify development, and you can also change the default values;
  • No XML configuration, no code generator, out of the box;
  • Easy to deploy, easy to maintain and upgrade;
  • Natural fit with microservices and cloud computing;

2. Introduction to micro services

In his paper published in 2014, Martin Fowler mentioned the concept of microservice as an architectural idea. The main idea is to split the functions of previous single applications into different services deployed on different servers, and each service communicates with each other through HTTP.

Martin Fowler’s paper: Microservices Papers

Single application

ALL IN ONE

All the services and functions are in the same project, which can be easily called between codes and deployed on one server. With the expansion of service functions, project modules become larger and more complex, which increases the cost of project development and maintenance. During upgrade and maintenance, the entire project needs to be redeployed.

Microservice applications

Develop different modules in the project in a divide-and-conquer manner to ensure that each functional element is always a software unit that can be replaced and upgraded independently. Each service module only focuses on specific business functions, and the business is clear with little code, which has been developed and maintained. Rapid system upgrade and iteration are supported. Exceptions of some functions do not affect the entire system.

Here is a brief introduction to the microservices architecture, but here is a blog post detailing the different architectures: Related blogs

3. Configure the environment

Jdk1.8 + : The official Spring Boot recommendation is JDK1.7 or higher

Maven 3.2+ : Spring is recommended to use Maven 3.2 or later

SpringBoot 2.3.4.RELEASE: The latest stable RELEASE of Spring

IDE: IntelliJ IDEA or STS(Spring Tool Suite)

For details about the relationship between Different Spring Boot versions, see: Relationship between Spring Boot Versions

For details about the configuration environment required by different Spring Boot versions, see the Spring Boot official documentation

The content about integrating MAVEN with IDEA is not described here. You are recommended to see the blog: CONFIGURING Maven in IDEA

4. Create a Spring Boot application

4.1 Creating a Vm using Maven

① Create maven project, package the jar package

② Write pom. XML file and import related dependencies

③ Write the main program class, write good annotations

④ Start the main project, browser access

Sample code:

1) pom. The XML file


      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.bruce</groupId>
    <artifactId>springBoot-01-helloword</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9. RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code

② Write the main program class: HelloWordMainApplication

/ * * *@SpringBootApplicationUsed to annotate a main program class, declared as a Spring Boot application */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) { SpringApplication.run(HelloWorldMainApplication.class, args); }}Copy the code

③ Create the Controller package and write the business code HelloController

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(a) {
        return "Hello SpringBoot";
    }

    @ResponseBody
    @RequestMapping("/test")
    public String test(a) {
        return "Test SpringBoot"; }}Copy the code

(4) Start the Spring Boot application, open the browser: localhost:8080/hello, and check the function written in HelloController

4.2 Quickly Creating a Spring Boot Application

Spring Boot projects can be quickly created using IDE tools. Take IntelliJ IDEA as an example:

Select the module we want, and the wizard will network to create a Spring Boot project, the default generated Spring Boot project. The main program is already generated, we just need our own logic. Specific steps:

(1) Select Spring Initializer for a new project

② Configure the environment related to the project

③ Select the required module

The detailed steps are captured in the screenshot

① Use Spring Inietializer to build projects

② Configure the project environment

③ Select the required modules

4 Enter the project name and path

The project structure after creation:

Directory structure in the Resources folder

  • Static: Saves all static resources. Js, CSS images;
  • Templates: Saves all the template pages (Spring Boot default JAR package uses embedded Tomcat, default does not support JSP page); You can use template engines (Freemarker, Thymeleaf);
  • Application. properties: Configuration file for the Spring Boot application. You can modify some default Settings;

5. Analyze applications created by Spring Boot

5.1 pom file

Create the pom.xml file that references the parent project

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4. RELEASE</version>
    <relativePath/> <! -- lookup parent from repository -->
</parent>
Copy the code

The parent project of pom.xml is spring-boot-starter-parent-xxx.pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4. RELEASE</version>
</parent>
Copy the code

Spring-boot-starter-parent-xxx. pom is a parent project of spring-boot-relains-xxx. pom, which manages all versions of spring Boot applications. Is the version arbitration center for Spring Boot. By default, we don’t need to write versions to import dependencies; (Dependencies that are not managed in dependencies still require a version number.)

Spring Boot Initiator

Spring-boot-starter -web: spring-boot-starter: the starter in the spring-boot scenario, which helps us import components that the required modules rely on for normal operation.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

This dependency is the Spring Boot scenario Starter, which is turned on.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.3.4. RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-json</artifactId>
        <version>2.3.4. RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.3.4. RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.9. RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.9. RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
Copy the code

In fact, each starter is the integration of related module dependencies. Spring Boot extracts all functional scenarios into one by one “Starters”. You only need to introduce all the dependencies related to these starter scenarios into the project. Import the scenario initiator for whatever functionality you want to use.

Starter is described in the Spring documentation: Reference link

5.2 Main program class

/ * * *@SpringBootApplicationTo mark a main program class, indicating that this is a Spring Boot application */
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        // Start the Spring applicationSpringApplication.run(DemoApplication.class, args); }}Copy the code

SpringBootApplication: The SpringBoot application annotation states on a class that this class is the main configuration class for SpringBoot, and SpringBoot launches the SpringBoot application by running the main method of that class.

@SpringBootApplicationComposition of annotations

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} )
public @interface SpringBootApplication {
Copy the code

① @springBootConfiguration: specify that the class is the configuration class of SpringBoot. This annotation is written on a class to indicate that this class is a Spring Boot configuration class;

  • @SpringBootConfigurationComments by@ConfigurationNote composition;
  • @ConfigurationUsed to mark a class as a configuration class. The configuration class can replace the configuration file, and the configuration class is also a component of the IoC container, so this annotation is provided by@ComponentNote composition.

② @enableAutoConfiguration: indicates that the Spring Boot automatic configuration function is enabled. Spring Boot can help you configure automatically. This annotation tells Spring Boot to enable automatic configuration.

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
Copy the code
  • @autoConfigurationPackage: An annotation for the automatic configuration package

  • @import: Spring’s underlying annotation used to Import a component into the container

    @ Import ({AutoConfigurationImportSelector. Class}) : The imported components by AutoConfigurationPackages. The Registrar. The class will be the main configuration class (@ SpringBootApplication annotation class) in the inside of the bag and all children under the bag all component scanning to the Spring container;

    • EnableAutoConfigurationImportSelector: selects which components to import. Return all components that need to be imported as full class names, and they will be added to the container. A lot of auto-configuration classes are imported into the containerxxxAutoConfigurationImport and configure the component names needed for the scenario into the container. With the automatic configuration class, we can save our own manual configuration class injection components and other work;
    • SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)Spring Boot obtains the values specified by EnableAutoConfiguration from the meta-INF/Spring. factories under the classpar and imports these values into the container. The auto-configuration class did everything we needed to configure ourselves.
    • J2EE’s overall integration solution and automatic configuration are thereSpring - the boot - autoconfigure - 2.3.4. RELEASE. The jarIn the package.

③ @ComponentScan: This annotation indicates that automatic packet scanning is enabled

Summary: The @SpringBootApplication annotation is a composite annotation that enables automatic configuration and component scanning of SpringBoot.

Here involves a lot of Spring annotations, the follow-up will open a pit to the commonly used in Spring annotations for detailed introduction, please look forward to (≧∇≦) Blue