Spring Boot项目实现热部署的完整指南

在Spring Boot开发过程中,实现代码热部署能够极大提升开发效率。当代码发生修改时,系统会自动重新加载并应用变更,无需手动重启服务。

引入热部署工具依赖

在项目配置文件中添加devtools依赖即可开启基础热部署功能:

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

添加此依赖后,每次修改Java源文件,应用都会自动重新编译并加载变更。

定制化热部署配置

以下配置项允许您根据需求调整热部署行为,均为可选设置:

# 启用或禁用热部署功能
spring.devtools.restart.enabled: true

# 指定需要监控变动的目录
# spring.devtools.restart.additional-paths: src/main/java

# 排除不需要监控的目录
spring.devtools.restart.exclude: test/**

IntelliJ IDEA环境配置

在IntelliJ IDEA中,需要完成以下两项设置才能确保热部署正常工作:

  1. 启用自动编译功能

    • 进入菜单:File > Settings > Build, Execution, Deployment > Compiler
    • 勾选”Build project automatically”选项
  2. 允许运行时自动构建

    • 按下快捷键:Ctrl + Shift + Alt + /
    • 选择”Registry”菜单
    • 勾选”compiler.automake.allow.when.app.running”选项

使用热部署的注意事项

  1. 生产环境识别:当以java -jar方式启动或使用自定义类加载器时,Spring Boot会将其识别为生产环境,自动禁用devtools功能。

  2. 打包行为控制:默认情况下,Maven打包不会包含devtools依赖。如需包含,需要显式配置Spring Boot Maven插件,禁用其
    excludeDevtools属性。

  3. 模板引擎缓存
    :使用Thymeleaf等模板引擎时,无需手动配置缓存禁用。devtools会自动设置相关属性,具体可参考
    完整属性列表。

以下是devtools自动配置的部分核心代码:

@Order(Ordered.LOWEST_PRECEDENCE)
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {

    private static final Map<String, Object> PROPERTIES;

    static {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("spring.thymeleaf.cache", "false");
        properties.put("spring.freemarker.cache", "false");
        properties.put("spring.groovy.template.cache", "false");
        properties.put("spring.mustache.cache", "false");
        properties.put("server.session.persistent", "true");
        properties.put("spring.h2.console.enabled", "true");
        properties.put("spring.resources.cache-period", "0");
        properties.put("spring.resources.chain.cache", "false");
        properties.put("spring.template.provider.cache", "false");
        properties.put("spring.mvc.log-resolved-exception", "true");
        properties.put("server.jsp-servlet.init-parameters.development", "true");
        PROPERTIES = Collections.unmodifiableMap(properties);
    }
  1. 进程管理问题:在Windows系统中,devtools可能会在资源管理器中保留Java进程。如果开发工具无法正常终止应用,可能需要手动结束进程,否则重启时会出现端口绑定冲突。

如需深入了解spring-boot-devtools的更多高级用法,请查阅
官方文档获取完整信息。