First day of learning SpringBoot

First day of learning SpringBoot

1. What is SpringBoot

Spring boot is designed based on spring 4.0. It not only inherits the original excellent features of spring framework, but also further simplifies the whole construction and development process of spring application by simplifying configuration. In addition, SpringBoot integrates a large number of frameworks to solve the version conflict of dependent packages and the instability of references.

Key benefits of SpringBoot

  • Get started faster for all Spring developers
  • Out of the box, various default configurations are provided to simplify project configuration
  • Inline containers simplify Web projects
  • There are no requirements for redundant code generation and XML configuration

2. The first SpringBoot program

2.1 environmental preparation

  • jdk1.8

  • Maven-3.6.1

  • The latest version of SpringBoot

  • IDEA

2.2 create basic project description

Spring Initializr: https://start.spring.io/

Project creation method 1:

  1. open https://start.spring.io/

  2. Fill in project information

  3. Click the "Generate Project" button to generate the project; download the project

  4. Unzip the project package and import it as Maven project with IDEA. Go to the next step until the project is imported.

Project creation method 2:

  1. Create a new project
  2. Select spring initalizr, and you can see that the default is to implement it in the quick build tool on the official website
  3. Fill in project information
  4. Select the initialized component (just check Web for beginners)
  5. Fill in the project path
  6. Wait for the project to build successfully

Project structure analysis:

  1. Main startup class of the program

  2. An application.properties configuration file

  3. A test class

  4. A pom.xml

3.pom.xml analysis

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhang</groupId>
    <artifactId>springboot-02-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-02-config</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>

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

</project>

There are four main parts:

  • Project Metadata: the Project Metadata entered during creation, that is, the basic elements of Maven project, including groupId, artifactId, version, name, description, etc

  • Parent: inherits the dependency management of spring boot starter parent, version control and packaging

  • Dependencies: project specific dependencies. Spring boot starter Web is used to implement the HTTP interface (Spring MVC is included in the dependency). The official website describes it as: using Spring MVC to build the Web (including RESTful) As the entrant to the application, Tomcat is used as the default embedded container. Spring boot starter test is used to write the dependency package of unit test. The use of more functional modules will be gradually expanded later.

  • Build: build the configuration part. Spring Boot Maven plugin is used by default. In combination with Spring Boot starter parent, Spring Boot applications can be packaged into jars to run directly.

4. Write an http interface

  1. Create a new controller package in the same level directory of the main program

  1. Create a new HelloController class in the package
@Controller
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}
  1. After writing, start the project from the main program, the browser initiates a request, and look at the page to return; the console outputs the port number accessed by Tomcat!

5. Special notes

5.1 changing the port number

5.2 change the alphabet of the characters displayed at startup

Just one step: create a new banner.txt in the resources directory under the project.
Patterns can be to: https://www.bootschool.net/ascii This website is generated, and then copied to the file!

6. Parent dependency

pom.xml

  • Spring boot dependencies: the core depends on the parent project!
  • When we write or introduce some Springboot dependencies, we do not need to specify the version, just because there are these version repositories
  1. It mainly depends on a parent project, which mainly manages the resource filtering and plug-ins of the project!
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  1. Click in, and there is a parent dependency
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.5.5</version>
  </parent>
  1. This is the place to really manage all dependent versions in the SpringBoot application, the SpringBoot version control center;

7. Spring boot starter

  • rely on
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
  • Springboot boot starter XXX is the springboot startup scenario

  • For example, spring boot starter web will help us automatically import all web dependencies

  • springboot will turn all functional scenarios into initiators one by one

  • If we want to use any function, we just need to find the corresponding initiator

8. Main program

8.1 default main startup program

package com.zhang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@Spring bootapplication to annotate a main program class
//This is a Spring Boot application
@SpringBootApplication
public class Springboot01HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01HelloworldApplication.class, args);
    }

}

8.2 annotation (@ SpringBootApplication)

  • Function: mark on a class to indicate that this class is the main configuration of SpringBoot
  • SpringBoot should run the main method of this class to start the SpringBoot application;
  • Enter this annotation: you can see that there are many other annotations above!
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)

@ComponentScan

  • This annotation is important in Spring and corresponds to the elements in the XML configuration.

  • Function: automatically scan and load qualified components or beans, and load the bean definition into the IOC container

@SpringBootConfiguration

Function: the configuration class of SpringBoot is marked on a class to indicate that it is a SpringBoot configuration class;

@EnableAutoConfiguration

Turn on the auto configuration function

  • Previously, we needed to configure things ourselves, but now SpringBoot can automatically configure them for us;
  • @EnableAutoConfiguration tells SpringBoot to enable the auto configuration function so that the auto configuration can take effect;

Tags: Java Spring Spring Boot

Posted on Tue, 28 Sep 2021 14:48:05 -0400 by bubbasheeko