配置IDEA

勾选​​Build project automatically​​选项。

Spring Boot:热部署_html

搜索​​Registry​​​(双击​​Shift​​​键),找到并勾选​​compiler.automake.allow.when.app.running​​选项。

Spring Boot:热部署_spring boot_02


新版本​​IDEA​​​(博主的​​IDEA​​​版本是​​IntelliJ IDEA 2021.3​​​)没有​​compiler.automake.allow.when.app.running​​选项。

Spring Boot:热部署_intellij-idea_03


新版本​​IDEA​​​将此选项迁移到了​​Advanced Settings​​(高级设置)中,勾选下图标记的选项即可。

Spring Boot:热部署_spring boot_04

配置Maven

添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

添加构建应用的插件:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

测试

​pom.xml​​:

<?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.6.2</version>
</parent>

<packaging>jar</packaging>

<groupId>com.kaven</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>springboot</name>
<description>springboot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

启动类:

package com.kaven.springboot;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringbootApplication.class)
.bannerMode(Banner.Mode.OFF)
.lazyInitialization(true)
.run(args);
}
}

接口:

package com.kaven.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

@GetMapping("/index")
public String index() {
return "index.html";
}
}

​HTML​​页面。

Spring Boot:热部署_java_05

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index.html</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index2.html</h1>
</body>
</html>

启动应用,访问​​http://localhost:8080/index​​。

Spring Boot:热部署_intellij-idea_06

修改Java代码

修改接口(不重新启动应用):

package com.kaven.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

@GetMapping("/index")
public String index() {
return "index2.html";
}
}

Spring Boot:热部署_intellij-idea_07


控制台也有日志输出。

Spring Boot:热部署_java_08

修改静态资源

修改静态资源(不重新启动应用):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index2.html kaven</h1>
</body>
</html>

Spring Boot:热部署_java_09

修改配置文件

修改默认配置(往配置文件​​application.properties​​中添加如下配置,不重新启动应用):

server.port=8081

之前的请求就无法访问了,因为应用的服务端口修改成了​​8081​​。

Spring Boot:热部署_spring boot_10


因此,需要访问​​http://localhost:8081/index​​。

Spring Boot:热部署_java_11


控制台也有日志输出。

Spring Boot:热部署_intellij-idea_12


​Spring Boot​​的热部署就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。