Java study notes-Day81 Spring Boot framework (1)


1. Introduction to Spring Boot


Whether it is the SSH framework set or the SSM framework set, the Spring framework is essential. The more traditional modes are in the form of applicationContext.xml configuration files. Of course, the most difficult and cumbersome configuration files are also these configuration files, and beginners are especially prone to errors. The Spring organization internally encapsulates the full range of components of the spring framework. Only maven (jar management, project packaging tool) or gradle (emerging jar management, project packaging tool) is provided externally to introduce parent.pom (maven configuration file) or parent.gradle (gradle configuration file), so that every Spring Projects are all run in the form of Spring sub-projects, so that developers no longer need to pay attention to the cumbersome configuration files but focus on business logic and deeper architecture. Since then, SpringBoot was born.

Two, the main advantages of Spring Boot


(1) Get started faster for all Spring developers.
(2) Out of the box, it provides various default configurations to simplify project configuration and help developers quickly integrate third-party frameworks (maven dependency features).
(3) The embedded container (web container Tomcat) simplifies the web project and can run without a third-party server at all. It has a built-in third-party container (tomcat/jetty/undertom) (Tomcat container is developed using java).
(4) There is no requirement for redundant code generation and XML configuration, and annotation is used to simplify XML writing.
(5) Provide a series of functional features of large-scale enterprise projects (such as: security, health detection, external configuration, database access, restful construction, etc.).

Three, create a Spring Boot project


(1) Click New -> click project -> select Spring Intializr -> select the Project SDK of the project -> Choose starter service URL and select Default (https://start.spring.io) -> click Next.

Insert picture description here

(2) Modify the Java Version to the corresponding version 8 (jdk1.8).

Insert picture description here
(3) Select Spring Web for Web and click Next.

Insert picture description here

(4) Enter the project name and project path, and click Finish to complete.
Insert picture description here
(5) Add the following dependencies to the pom.xml file of the project.

<?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.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
           <!-- starter-web web相关的启动器 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- starter-test test相关的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

(6) The entrance of springboot should be in the outer package of the controller. Create the Spring Boot entry program class in the src\main\java\com\example\demo package, create the controller package in the demo, and create the controller class in the controller.

  • DemoApplication.java (Entry program class of Spring Boot)
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

  • HelloSpringBoot.java (controller class)
package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpringBoot {
    
    

    @GetMapping("hello")
    public String getHello(){
    
    
        return "Hello,Spring Boot!";
    }

}

(7) Right-click in the entry program class -> Run DemoApplication, the console output is shown below, which proves that the Spring Boot project has been successfully run.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42141141/article/details/115057489