Spring Data Elasticsearch @Document indexName 在运行时定义 [英] Spring Data Elasticsearch @Document indexName defined at runtime
问题描述
是否可以(在运行时)为每个 @Document
动态指定 indexName
,例如,通过配置文件?或者是否可以使 @Document
依赖 Spring 环境(dev、prod)?
Is it possible to specify dynamically (at runtime) the indexName
for each @Document
, for example, via a configuration file? Or is it possible to make @Document
Spring environment (dev, prod) dependant?
谢谢!
推荐答案
@Document 注解不允许直接在参数中传递索引名.不过我找到了解决方法.
The @Document annotation does not permit to pass the indexname in parameter directly. However I found a work around.
在我的配置类中,我创建了一个返回字符串的 Bean.在这个字符串中,我用 @Value 注入了索引的名称:
In my configuration class I created a Bean returning a string. In this string I injected the name of the index with @Value :
@Value("${etrali.indexname}")
private String indexName;
@Bean
public String indexName(){
return indexName;
}
之后可以像这样将索引注入@Documentation 注解:
Afterward it is possible to inject the index into the @Documentation annotation like this :
@Document(indexName="#{@indexName}",type = "syslog_watcher")
对我有用,希望对你有帮助.
It works for me, I hope it will help you.
最好的问候
这篇关于Spring Data Elasticsearch @Document indexName 在运行时定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!