Spring boot quick start

Posted by Mistah Roth on Fri, 11 Feb 2022 14:47:18 +0100

Getting started SpringBoot

SpringBoot is a new framework provided by pivot team. It is designed to simplify the initial construction and development process of Spring applications

quick get start

1. Create a new module interface

2. Check SpringWeb under Web. For spring MVC dependency, click Finish

3. Write a simple business layer code

@RestController
@RequestMapping("/books")
public class BookController {

    @GetMapping
    public String getById(){
        System.out.println("spring boot is running");
        return "spring boot is running";
    }
}

4. Start springbootapplication Java startup class

5. Access URL:http://localhost:8080/books

The basic file of SpringBoot starter

pom.xml

Application class

Comparison between SpringBoot and Spring

Class / profileSpringBootSpring
Coordinates in pom fileCheck addAdd manually
web3.0 configuration classnothingHandmade
Spring / spring MVC configuration classnothingHandmade
controllerHandmadeHandmade

Create and import the SpringBoot project on the official website

1. Official website address https://start.spring.io

2. Create a project on the official website and obtain the compressed package.

3. Unzip it to the local working directory

Alibaba cloud creates SpringBoot

Modify the Server URL to: http://start.aliyun.com

Creating projects in a non networked way

1. First, make sure that Maven warehouse has the following dependencies, manually create Maven project and write POM XML file

<?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 http://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.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zut</groupId>
    <artifactId>springboot_01_03_quickstart</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

2. Write startup class

@SpringBootApplication//Necessary notes
public class Application {

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

3. Write the controller and access it through the browser

Introduction case analysis

■Parent

SpringBoot can use parent dependency management < dependency manager > to version control the dependencies we need.

When some dependencies are not introduced into the Parent and you need them, you still need to specify the version by yourself. Pay attention to the problem of version conflict.

When we introduce dependency, we can directly use the version given by the parent project to achieve the effect of unified version.

Alibaba cloud inherits from the parent cloud. Inherit only once.

Note: different SpringBoot versions correspond to different dependent versions

■ Starter (dependency passing of SpringBoot)

Each Starter introduces the dependencies involved in related technologies. We just need to select the Starter reference according to the technology.

■ guided Application

Guide the main method in the class. We can find it by obtaining the return value of the following methods:

ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);

ConfigurableApplicationContext: context object, i.e. configuration file.

		ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
        BookController bean = run.getBean(BookController.class);
        System.out.println(bean);
//com.zut.controller.BookController@5e76a2bb

The initialization and configuration of the container are completed by the Application.

Note: by default, the annotation scanning range is the package where the startup class is located and its sub packages.

■ principle of starting Tomcat

1. Open POM XML, find the spring web starter and enter

2. Find the starter of tomcat and enter

3. There will be embedded tomcat dependencies

Remove tomcat test

1. Exclude tomcat dependency

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. Start the project

The tomcat server is no longer started.

3. Replace jetty server test

<!--introduce jetty-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Three built-in servers of SpringBoot

tomcat

Produced by apache, it has many fans and a wide range of applications. It is loaded with several heavy components.

jetty

Jetty is lighter and more scalable than Tomcat (compared with Tomcat). Google application engine (GAE) has been fully switched to jetty, and the load performance is far lower than Tomcat

undertow

undertow, load performance barely outperforms tomcat

Topics: Java Spring Spring Boot