文章目录

  • 一、创建Maven工程
  • 二、添加SpringBoot的起步依赖
  • 三、编写SpringBoot引导类
  • 四、编写Controller
  • 五、测试
  • 六、SpringBoot工程热部署


一、创建Maven工程

springboot入门 springboot入门要多久_springboot入门

springboot入门 springboot入门要多久_spring_02


springboot入门 springboot入门要多久_热部署_03

springboot入门 springboot入门要多久_java_04

二、添加SpringBoot的起步依赖

文件位置:pom.xml

SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>

SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖

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

修改后配置文件如下:

springboot入门 springboot入门要多久_热部署_05

三、编写SpringBoot引导类

要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问

新建类:MySpringBootApplication.java
文件位置:java/com/itheima/MySpringBootApplication.java

package com.itheima;

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

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

SpringApplication.run(MySpringBootApplication.class) 代表运行SpringBoot的启动类,参数为SpringBoot启动类的字节码对象。

添加后如下:

springboot入门 springboot入门要多久_热部署_06

四、编写Controller

在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController

新建类:QuickController.java
文件位置:java/com/itheima/Controller/QuickController.java

package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class QuickController {
    @RequestMapping("/quick")
    @ResponseBody
    public String quick(){
        return "hello springboot";
    }
}

新建如下:

springboot入门 springboot入门要多久_热部署_07

五、测试

执行SpringBoot起步类的主方法,控制台打印日志如下:

springboot入门 springboot入门要多久_热部署_08


通过日志发现,Tomcat started on port(s): 8080 (http) with context path ‘’

tomcat已经起步,端口监听8080,web应用的虚拟工程名称为空

打开浏览器访问url地址为:http://localhost:8080/quick

springboot入门 springboot入门要多久_热部署_09

六、SpringBoot工程热部署

我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大
量的时间,我们可以在修改代码后不重启就能生效,在 pom.xml 中添加如下配置就可以实现这样的功能,我们称
之为热部署。

<!--热部署配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

注意:IDEA进行SpringBoot热部署失败原因

出现这种情况,并不是热部署配置问题,其根本原因是因为Intellij IEDA默认情况下不会自动编译,需要对IDEA进

行自动编译的设置,如下:

springboot入门 springboot入门要多久_spring_10

然后 Shift+Ctrl+Alt+/,选择Registry

springboot入门 springboot入门要多久_springboot入门_11