在@Document indexName 中使用的 SpEL 与 spring 数据 elasticsearch 和 spring boot 没有被解析 [英] SpEL used in @Document indexName with spring data elasticsearch and spring boot is not being parsed

查看:41
本文介绍了在@Document indexName 中使用的 SpEL 与 spring 数据 elasticsearch 和 spring boot 没有被解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Document 注释中使用 SpEL 寻找一些帮助,参考:

looking for some help using the SpEL inside @Document annotation in reference to:

spring-data-elasticsearch:3.2.3.RELEASE 和 spring boot 2.2.1 RELEASE

spring-data-elasticsearch:3.2.3.RELEASE and spring boot 2.2.1 RELEASE

我在用谷歌搜索帮助解决这个问题时遇到了麻烦,因为关键字选择了不相关的问题(我已经看到了 关于动态 indexName 的其他(未回答)问题).

i am having trouble googling for help with this problem as the keywords pick up unrelated questions (i have seen the other (unanswered) question about dynamic indexName).

我想设置

@Document(indexName = "${es.index-name}", ...)

使用从我的 application.properties 中写入的属性 (es.index-name) 值派生的 indexName 值.

with the value for the indexName derived from a property (es.index-name) value written in my application.properties.

它使用文字字符串值 "${es.index-name}" 作为索引名称!

it is instead using the literal String value "${es.index-name}" as the index name!

我也尝试过创建一个名为 EsConfig

i have also tried creating a @Component called EsConfig

带有 indexName 注释的字段 @Value("${es.index-name}")

with a field indexName annotated with @Value("${es.index-name}")

然后尝试使用 SpEL 访问此组件属性值:

and then trying to access this component property value using SpEL:

@Document(indexName = "#{esConfig.indexName}", ...)

但这也不起作用(仍然解析为文字字符串并抱怨大写).我已经通过调试器确认 EsConfig 组件正在正确解析 SpEL 并提供正确的值.但到达 @Document

but this does not work either (still parsing as literal String and complains of uppercase). i have confirmed through the debugger that the EsConfig component IS parsing the SpEL correctly and providing the right value. but it fails when reaching @Document

这里是完整的代码片段:

here are the full code snippets:

使用 @Document 和 SpEL 访问 application.properties

using @Document with SpEL accessing application.properties

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Document(indexName = "${es.index-name}", type = "tests")
public class TestDocument {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private String id;
}

EsConfig 数据源组件(尝试使用和不使用 Lombok)

EsConfig data source Component (tried with and without using Lombok)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("esConfig")
public class EsConfig {
  @Value("${es.index-name}")
  private String indexName;

  public String getIndexName() {
    return indexName;
  }

  public void setIndexName(String indexName) {
    this.indexName = indexName;
  }
}

使用 @Document 和 SpEL 访问 EsConfig indexName 属性

using @Document with SpEL accessing EsConfig indexName property

@Data
@Document(indexName = "#{esConfig.indexName}", type = "tests")
public class TestDocument {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private String id;
}

推荐答案

使用名称和方法引用您的 bean:

Reference your bean with the name and method:

@Document(indexName = "#{@esConfig.getIndexName()}")

这篇关于在@Document indexName 中使用的 SpEL 与 spring 数据 elasticsearch 和 spring boot 没有被解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆