美文网首页
如何在不重启服务器的情况下在 Spring Boot 上重新加载

如何在不重启服务器的情况下在 Spring Boot 上重新加载

作者: 邢_3941 | 来源:发表于2021-11-07 12:14 被阅读0次

介绍
当您需要加快开发工作而不必担心在应用程序中的代码片段的每次更改时重新启动服务器时,可能会出现这种情况。Spring Boot 提供了这样的规定,您可以使用它轻松实现目标。

每当 classpath 上的文件发生更改时,使用spring-boot-devtools 的应用程序将自动重新启动。在 IDE 中工作时,这可能是一个有用的功能,因为它为代码更改提供了非常快速的反馈循环。默认情况下,将监视指向文件夹的类路径上的任何条目的更改。

使用此依赖项保存的任何更改,嵌入式 tomcat 将重新启动。Spring Boot 有一个开发者工具(devtools)模块有助于提高开发者的生产力。

先决条件
Spring Boot 2.2.2 – 2.4.5,Spring Boot Devtools,Java 至少 8

无需重新启动服务器即可重新加载代码更改
让我们在 Eclipse IDE 中创建 Spring Boot 项目。该项目的名称是spring-boot-reload-changes-without-server-restart。

该的build.gradle下面的脚本给出与基于gradle这个项目所需的依赖关系:

buildscript {
    ext {
        springBootVersion = '2.2.2.RELEASE' to 2.4.5
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    implementation("org.springframework.boot:spring-boot-devtools:${springBootVersion}")
}

下面给出了基于 maven 的项目对应的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.roytuts</groupId>
    <artifactId>spring-boot-reload-changes-without-server-restart</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>

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

    <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>
        </dependency>
    </dependencies>

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

现在我将编写主类来启动 Spring Boot 应用程序:

package com.roytuts.spring.boot.reload.changes.without.server.restart;

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

@SpringBootApplication
public class SpringBootChangesWithoutRestartServerApp {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootChangesWithoutRestartServerApp.class, args);
    }

}

现在运行上面的主类将部署您的应用程序,服务器将在端口 8080 上启动。

现在尝试访问根 URL http://localhost:8080http://localhost:8080/greet,您将在浏览器上看到以下输出:
接下来,我将使用/greet端点创建以下 Spring REST 控制器类。

package com.roytuts.spring.boot.reload.changes.without.server.restart;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/greet")
    public String greet() {
        return "Greetings, Welcome!";
    }

}

现在,每次保存代码更改时,嵌入式 Tomcat 服务器都会自动重新初始化。

现在点击 URL http://localhost:8080/greet会给你以下输出:
所以你知道如何在不重启服务器的情况下在 Spring Boot 应用程序中重新加载我们的代码更改。

相关文章

网友评论

      本文标题:如何在不重启服务器的情况下在 Spring Boot 上重新加载

      本文链接:https://www.haomeiwen.com/subject/qwjsaltx.html