【问题标题】:Spring - Thymeleaf - Hot reload of message files (localisation)Spring - Thymeleaf - 消息文件的热重载(本地化)
【发布时间】:2022-01-01 12:15:21
【问题描述】:

我正在使用带有 devtools 依赖项的 spring boot (2.5.7) 来进行热重载。它工作得很好(包括片段的变化),但不适用于本地化文件(资源/语言下的message_XX.properties)。每次我在那里进行更改时,我都需要重新启动服务器。这是我的 application.yaml:

spring:
  thymeleaf:
    cache: false
    mode: HTML
    encoding: UTF-8
    prefix: file:src/main/resources/templates/
  web:
    resources:
      static-locations:
        - file:src/main/resources/static/
      cache:
        period: 0

一些修改:

  • 我使用 vscode 和 gradle 7
  • 我重新定义了MessageSource

有什么想法吗?

谢谢!

【问题讨论】:

  • 您的位置错误,没有src/main/resources,您实际上正在覆盖默认值(已经指向那里)。尽管您确实更改了默认值(或者甚至声明了您自己的 MessageSource 而不是使用默认值),但您的配置中没有任何关于消息的内容。
  • @DoNhuVy,我没有使用 IntelliJ,所以这无济于事(我使用 VSCode,但手动构建有等级的东西)。
  • @M.Deinum,是的,我重新定义了 MessageSource 以指向 lang 文件夹。但我确实有一个文件夹src/main/resources,其中有statictemplates 文件。如果我从 yaml 文件中删除该行,则热重载不适用于模板文件。
  • 取决于MessageSource 缓存可能会或可能不会起作用。您应该将缓存时间设置为 0(无缓存)。 src/main/resources 是类路径。如果您离开此配置并部署 jar 文件(或 war),您的应用程序将不再运行。禁用缓存并在 IDE 中进行自动构建应该可以重新加载。如果您使用的是 Intellij 或 netbeans,请参阅 attacomsian.com/blog/…(方法 1)。

标签: spring-boot thymeleaf hot-reload


【解决方案1】:

之前忘记回复了,最后干脆回了一句:

spring:
  thymeleaf:
    cache: false
    mode: HTML
    encoding: UTF-8
  cache:
    period: 0

我的LocaleConfig 班级:

@Configuration
public class LocaleConfig {
    @Bean
    public AcceptHeaderLocaleResolver localeResolver() {
        final AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
        resolver.setDefaultLocale(Locale.ENGLISH);
        return resolver;
    }

    @Bean
    public ResourceBundleMessageSource messageSource() {
        final ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasename("lang/message");
        return source;
    }
}

然后,我需要两个终端,一个从 gradle 连续构建,另一个使用 spring boot bootRun 任务:

./gradlew compileJava --continuous

./gradlew bootRun

免责声明:我可能需要停止并重新启动两者,因为有时不会收到新消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 2015-12-06
    • 2014-01-14
    • 2023-03-24
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多