让SpringBoot工程支持热部署

下载地址:https://files.cnblogs.com/files/xiandedanteng/SpringBootWeb-1_20190928.rar

修改Java文件后,每次要重启才好用,修改一下pom.xml,增加一个dependency,可以让Tomcat容器重新加载修改java类的class,这样能做到热部署。

这个denpendency就是spring-boot-devtools,具体写法如下:

<?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.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hy</groupId>
    <artifactId>SpringBootWeb-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootWeb-1</name>
    <description>Demo project for Spring Boot</description>

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

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

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

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

</project>

增加这个依赖后,在控制器类里再增加一个跳转,

    @RequestMapping("/hot")
    public String hot() {
        return "new.html";
    }

再把要跳转的网页准备好:

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>标题</title>
    </head>

     <body>
            <h2>热部署</h2>
     </body>
</html>
<script type="text/javascript">
<!--
    // 脚本
//-->
</script>

准备完毕不用重启,直接在浏览器的地址栏中输入http://localhost:8080/hot

以下页面就会出现:

不用重启Tomcat,确实方便不少。

--END-- 2019年9月28日08:00:44

原文地址:https://www.cnblogs.com/heyang78/p/11601341.html