【问题标题】:How do I enable Thymeleaf Live Reloading in Spring Boot 1.3如何在 Spring Boot 1.3 中启用 Thymeleaf 实时重新加载
【发布时间】:2016-04-11 20:11:29
【问题描述】:

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

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

但不知何故,它仍然没有自动重新加载。我必须先点击“制作项目”按钮。我有另一个项目,具有相同的配置(不确定 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'
}

想法?

【问题讨论】:

    标签: intellij-idea gradle spring-boot thymeleaf


    【解决方案1】:

    由于您使用的是 Spring Boot 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')
    }
    

    【讨论】:

    • 我没有在这两个项目上使用 spring-boot-devtools。如果我不需要,不愿意添加另一个依赖项。
    【解决方案2】:

    Eclipse 将在保存时自动编译项目。 IntelliJ 没有。编译操作是触发重新加载的原因。作为一个 IntelliJ 用户,我觉得这很烦人。

    我对 devtools 进行了审查,并在此处重新加载了我的博客:https://springframework.guru/spring-boot-developer-tools/

    【讨论】:

    • 我有 2 个项目,一个有效,一个无效。在有效的项目中,它似乎没有编译。它应该从源(而不是 jar)动态加载模板,因此保存和刷新工作得非常好。我不知道为什么它不适用于我的第二个项目。也许我应该创建一个新项目并重新导入代码。
    【解决方案3】:

    根据Spring Boot 1.3 release documentation

    Spring Boot Gradle 插件不再添加src/main/resources 使用 bootRun 时直接到类路径。如果你想活, 我们建议使用 Devtools 进行就地编辑。添加资源 如果要恢复 Spring,可以在 gradle build 中设置属性 启动 1.2。行为。

    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 使用的那样在快速重新加载的基础上工作。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2018-08-29
    • 2019-05-29
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多