Hibernate Search 的外部化配置

Externalizing configuration for Hibernate Search

我正在 运行 休眠搜索 spring 启动。我已经为我的应用程序编写了一个工作配置。然而,我想外部化我的配置并使用 ./config/hibernate.properties 而不是 src/main/resources/hibernate.properties。将属性文件复制到所需位置后,出现异常:

nested exception is java.io.FileNotFoundException: class path resource [hibernate.properties] cannot be opened because it does not exist

有人知道我应该如何告诉 spring 读取我的配置文件吗?

将您的配置移动到 src/main/resources/application.properties 文件并在所有地方添加 spring.jpa.properties.,例如 hibernate.dialect 将变为 spring.jpa.properties.hibernate.dialect

然后您可以使用 Spring 功能将您的配置移动到任何您想要的位置。要将其移动到 ./config/application.properties,我想您必须将 @PropertySource("./config/application.properties") 添加到您的 @Configuration 类 之一或类似的东西。

我相信您也可以将休眠配置保存在一个单独的文件中(与应用程序配置的其余部分分开)。

有关 Spring 引导中外部化配置的更多详细信息,请参阅 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

出于某种原因,只要 hibernate.properties 配置文件不存在,hibernate-search 似乎就会阻止应用程序启动。在尝试了一段时间没有成功之后,我找到了解决我的问题的方法。

  • 首先,我创建了一个空的 hibernate.properties 文件并将其放在 src/main/resources.
  • 其次,我将所有休眠搜索配置移动到 application.properties,如下所示:

    spring.jpa.properties.hibernate.search.default.indexmanager = elasticsearch spring.jpa.properties.hibernate.search.default.elasticsearch.host = http://my-server.com spring.jpa.properties.hibernate.search.default.elasticsearch.index_schema_management_strategy = CREATE spring.jpa.properties.hibernate.search.default.elasticsearch.required_index_status = yellow

这样,应用程序将启动,并且 spring 将从记录的外部化配置中获取所有配置 here