如何在 Spring Boot 1.3 中启用 Thymeleaf Live Reloading

How do I enable Thymeleaf Live Reloading in Spring Boot 1.3

我创建了一个使用 Thymeleaf 的 Spring Boot Gradle 项目。我的 IDE 是 IntelliJ。我在根文件夹中创建了一个 application.properties:

spring.resources.cache-period=0
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5

但不知何故它仍然没有自动重新加载。我必须先点击 "Make Project" 按钮。我有另一个项目,具有相同的配置(不确定 IntelliJ 设置),奇怪的是,在刷新时确实有效。

我的 application.properties 正在被读取,因为我可以使用 @Value 注释拉出自定义 属性。

作为参考,我的build.gradle

buildscript {
    ext {
        springBootVersion = '1.3.1.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.springframework:springloaded:1.2.5.RELEASE")
    }
}

apply plugin: 'spring-boot'
apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = 1.8
targetCompatibility = 1.8

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

jar {
    baseName = 'earthalive'
    version = ""
}

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('net.sourceforge.nekohtml:nekohtml:1.9.22')
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

想法?

由于您使用的是 Spring 启动的 1.3 - 也许其他项目正在使用 devtools - 它会自动刷新 springBoot 应用程序:

compile 'org.springframework.boot:spring-boot-devtools'

spring-boot-devtools 是使用 Thymeleaf 对我来说非常棒的原因 - 结合 Live Reload chrome/firefox 扩展,您甚至不必刷新浏览器。 Spring docs

dependencies {
    compile 'org.springframework.boot:spring-boot-devtools'
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('net.sourceforge.nekohtml:nekohtml:1.9.22')
}

Eclipse 会在保存后自动编译项目。 IntelliJ 没有。编译操作触发重新加载。作为 IntelliJ 用户,我觉得这很烦人。

我在我的博客上查看了开发工具并重新加载:https://springframework.guru/spring-boot-developer-tools/

根据 Spring Boot 1.3 release documentation:

The Spring Boot Gradle plugin no longer adds src/main/resources directly to the classpath when using bootRun. If you want live, in-place editing we recommend using Devtools. The addResources property can be set in your gradle build if you want to restore Spring Boot 1.2. behaviour.

Thymeleaf 依赖于 src/main/resources 添加到类路径 不管 是否使用 spring-boot-devtools。幸运的是,spring-boot-devtools 确实有一个选项可以再次打开它以恢复引导 1.2 行为。

添加到您的 build.gradle:

https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html

bootRun {
    addResources = true
}

个人意见:在我看来 Spring-loaded 最终会被弃用,取而代之的是 spring-boot-devtools。动态热交换在 Spring 中似乎是一件复杂的事情,我认为 Spring 团队已经决定宁愿像 devtools 使用的那样在快速重新加载的基础上工作。