Spring Boot 简介及快速搭建

  1. 导入依赖:

    <!-- 继承 SpringBoot 的父项目 -->
    <!-- spring boot 要加上下面的 parent 代码才能将当前项目标记为 spring boot 项目 -->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.6</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    
    <!-- 下面的 plugin 中的 spring-boot-maven-plugin 可以告诉服务器 SpringBoot 的启动类在哪 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  2. 建包并创建控制器

    @RestController  // 相当于 @Controller + @ResponseBody
    @RequestMapping("/hello")
    public class HelloController {
    
        @RequestMapping("/world")
        public String sayHi() {
            return "hello world!";
        }
    
    }
  3. 编写启动类

    @SpringBootApplication      // 标记为 SpringBoot 的启动类  整个工程只有一个
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
  4. 修改端口及添加项目名

    在resource文件夹中创建一个 application.properties 文件(文件名只能是这个)

    # 端口
    server.port=8080
    # 项目虚拟路径
    server.servlet.context-path=/wyu

    ​ server.port 是用来选用端口的

    ​ server.servlet.context-path 则是用来添加项目名,注意:没有使用 server.servlet.context-path 之前的访问地址为localhost:8080/hello/world,使用之后地址变为 localhost:8080/wyu/hello/world

  5. 部署服务器

    在 pom.xml 文件中加入下面的代码

    <!-- 下面的 plugin 中的 spring-boot-maven-plugin 可以告诉服务器 SpringBoot 的启动类在哪 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  6. 代码讲解

    <!-- 引入父 Maven 项目,继承父 Maven 项目中所有的配置信息
        spring-boot-starter-parent: 又引入一个父 Maven 项目
        <parent>
            <artifactId>spring-boot-dependencies</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.6.6</version>
        </parent>
        spring-boot-dependencies 帮我们管理了 SpringBoot 应用中所有的依赖和版本
                以后我们导入已有依赖就不需要写版本号了,它帮我们解决了第三方库之间的版本冲突问题
                名次:SpringBoot 的版本仲裁中心
    -->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.6</version>
    </parent>
    
    <!--
        starter 场景启动器:不同场景启动器维护了所对应的所有依赖,从而简化 maven 文件书写。
        spring-boot-starter-web:使用 Spring MVC 构建 Web (包括RESTful)应用程序。
            使用 Tomcat 作为默认的嵌入式容器
    -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    
    <!-- 下面的 plugin 中的 spring-boot-maven-plugin 可以告诉服务器 SpringBoot 的启动类在哪 -->
    <!-- 部署 SpringBoot 的插件,只有加了这个插件,当运行 java -jar xxx.jar 才能正常启动 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  7. 面试题:

    • 描述一下 SpringBoot 的作用
    • SpringBoot 有哪些特性?