spring-data-elasticsearch,spring-el,Spring Boot,Spring Annotations,spring Data Elasticsearch,Spring El" /> spring-data-elasticsearch,spring-el,Spring Boot,Spring Annotations,spring Data Elasticsearch,Spring El" />

Spring boot 未分析@Document indexName中使用的SpEL以及spring数据elasticsearch和spring引导

Spring boot 未分析@Document indexName中使用的SpEL以及spring数据elasticsearch和spring引导,spring-boot,spring-annotations,spring-data-elasticsearch,spring-el,Spring Boot,Spring Annotations,spring Data Elasticsearch,Spring El,正在使用SpEL inside@Document注释查找有关以下内容的帮助: spring数据弹性搜索:3.2.3.发布和弹簧靴2.2.1发布 我在谷歌上搜索这个问题的帮助时遇到了麻烦,因为关键字会选择不相关的问题(我已经看到了) 我想定一个时间 @Document(indexName=“${es.index name}”,…) 使用从我的应用程序.properties中写入的属性(es.index name)值派生的index name值 而是使用文本字符串值“${es.index name}

正在使用SpEL inside
@Document
注释查找有关以下内容的帮助:

spring数据弹性搜索:3.2.3.发布
和弹簧靴
2.2.1发布

我在谷歌上搜索这个问题的帮助时遇到了麻烦,因为关键字会选择不相关的问题(我已经看到了)

我想定一个时间

@Document(indexName=“${es.index name}”,…)

使用从我的
应用程序.properties
中写入的属性(
es.index name
)值派生的
index name

而是使用文本字符串值
“${es.index name}”
作为索引名

我还尝试创建一个名为
EsConfig

带有一个字段
indexName
并用
@Value(${es.index name})注释

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

@Document(indexName=“#{esConfig.indexName}”,…)

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

以下是完整的代码片段:

使用
@Document
并使用SpEL访问
应用程序.properties

导入lombok.数据;
导入org.springframework.data.elasticsearch.annotations.Document;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
@资料
@文档(indexName=“${es.index name}”,type=“tests”)
公共类测试文档{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有字符串id;
}
EsConfig数据源组件
(使用或不使用Lombok进行了尝试)

import org.springframework.beans.factory.annotation.Value;
导入org.springframework.stereotype.Component;
@组件(“esConfig”)
公共类EsConfig{
@值(${es.index name}”)
私有字符串indexName;
公共字符串getIndexName(){
返回indexName;
}
public void setIndexName(字符串indexName){
this.indexName=indexName;
}
}
使用带有SpEL的
@Document
访问
EsConfig
indexName
属性

@数据
@文档(indexName=“#{esConfig.indexName}”,type=“tests”)
公共类测试文档{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有字符串id;
}

使用名称和方法引用bean:

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

我的上帝,谢谢你。这已经让我疯狂了好几天了你知道为什么“${es.index name}”和“#{esConfig.index name}”都不起作用吗?index name参数是用需要
#{…}
的SpelExpressionParser解析的。在该表达式中,您需要使用
@
引用bean,并且需要使用getter方法,无direkt属性访问。您可以尝试
{es.index name}
,但我不确定解析器是使用环境tn还是只使用可用的bean。我明白了。让我困惑的是,我的朋友使用的是spring boot 2.1.x+spring data elasticsearch 3.1.x,对他来说@Document(indexName=“#{esConfig.indexName}”)确实有效。我不明白为什么只是把小版本升级会产生如此巨大的差异。变更日志中没有反映这一点。再次感谢您的帮助我花了几个月的时间尝试为不同的环境动态设置索引名。谢谢你的解决方案!