Spring boot 第1章预习

Spring Boot 概述

1.spring boot 概述

spring boot 是基于spring开发的全新框架,简化spring的初始化搭建和开发过程。

2.开发环境

  • 环境准备(安装以下软件)
    • jdk1.8 版本在以上
    • Apache Maven 3.6.0 以上
    • IntelliJ IDEA Ultimate
  • 环境配置(idea 中maven 项目的配置)
    • jdk 配置 File 》Project Structure 》Project 》
      • 设置SDK为1.8
    • Maven 配置 File 》 settings 》Maven 》
      • 设置Maven Home 为maven的安装目录
      • 设置user setting file 为maven 安装目录下的conf 文件夹下的settings.xml 文件
      • 设置Local repository 为maven 仓库的位置,maven 仓库的位置任意,便于找到可以将仓库目录放到maven 的安装目录下

3.创建spring boot 项目

  • 使用maven 创建

    1. 创建maven 项目(可以创建项目后再完成开发环境中的环境配置

    2. pom.xml 中添加spring boot 相关依赖

        <!-- 引入Spring Boot依赖 -->
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.2.4.RELEASE</version>
          </parent>
          <dependencies>
              <!-- 引入Web场景依赖启动器 -->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
          </dependencies>
      
    3. 编写主程序启动类

      注意:编写之前点击maven管理窗口的toggle offline mode 使得dependency 可用

      package cn.edu.sjzc;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      @SpringBootApplication
      public class ManualChapter01Application {
      
          public static void main(String[] args) {
              SpringApplication.run(ManualChapter01Application.class);
          }
      }
      
      
    4. 创建一个用于web 访问的controller

      package cn.edu.sjzc.controller;
      
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class HelloController {
      
          @GetMapping("/hello")
          public String hello(){
              return "hello spring boot";
          }
      }
      
      
    5. 运行项目

      运行主程序启动类中的main 方法

  • 使用spring initializr 创建

    1. 创建spring boot 项目

      勾选spring web

    2. 创建一个用于web 访问的controller

      controller 包要和启动类处于同一个包中(或者启动类所在包的子包中)

      注意:编写之前点击maven管理窗口的toggle offline mode 使得dependency 可用,并且上述环境配置正确

      package cn.edu.sjzc.manual_test02.controller;
      
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class HelloController {
      
          @GetMapping("/hello2")
          public String hello(){
              return "hello spring initializr spring boot";
          }
      }
      
      
    3. 运行项目

      运行启动类中的main 方法

4.单元测试

​ 注意:spring initializr 方法创建时自动生成

  1. 在pom 文件中添加spring-boot-start-test 测试启动器
  2. 编写单元测试类(把要测试的类设置为私有属性,并依赖注入)
  3. 编写单元测试方法(补充要测试的内容)
  4. 运行结果(运行测试方法)

5.springboot 热部署

  1. 在pom 文件中添加spring-boot-devtools 热部署依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
    
  2. 在idea中热部署配置

    • 编译配置 File 》 setting 》compiler 》 勾选 Build project automatically
    • 按快捷键Shift+Ctrl+Alt+/ 》选择1.registry 》 勾选 compiler.automake.allow.when.app.running
  3. 热部署测试

​ 修改代码,在不重启的情况下重新访问地址,结果发生变化

发布了25 篇原创文章 · 获赞 0 · 访问量 1280

猜你喜欢

转载自blog.csdn.net/weixin_42059368/article/details/104541408