配置IDEA
勾选Build project automatically
选项。
搜索Registry
(双击Shift
键),找到并勾选compiler.automake.allow.when.app.running
选项。
新版本IDEA
(博主的IDEA
版本是IntelliJ IDEA 2021.3
)没有compiler.automake.allow.when.app.running
选项。
新版本IDEA
将此选项迁移到了Advanced Settings
(高级设置)中,勾选下图标记的选项即可。
配置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
页面。
<!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
。
修改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";
}
}
控制台也有日志输出。
修改静态资源
修改静态资源(不重新启动应用):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index2.html kaven</h1>
</body>
</html>
修改配置文件
修改默认配置(往配置文件application.properties
中添加如下配置,不重新启动应用):
server.port=8081
之前的请求就无法访问了,因为应用的服务端口修改成了8081
。
因此,需要访问http://localhost:8081/index
。
控制台也有日志输出。
Spring Boot
的热部署就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。