Spring Data Elasticsearch @Document indexName defined at runtime

Is it possible to dynamically (at run time) specify indexName for each @Document , for example, through a configuration file? Or can you make a dependency of the @Document Spring environment (dev, prod)?

Thanks!

+5
source share
2 answers

@Document annotations do not directly pass the index name in a parameter. However, I found a job around.

In my configuration class, I created a Bean by returning a string. On this line, I entered the index name using @Value:

 @Value("${etrali.indexname}") private String indexName; @Bean public String indexName(){ return indexName; } 

After that, you can insert the index into the @Documentation annotation as follows:

 @Document(indexName="#{@indexName}",type = "syslog_watcher") 

It works for me, hope it helps you.

Best wishes

+7
source

The solution from Bruno probably works, but โ€œI created a Bean part returning a stringโ€ is a bit confusing.

Here is how I do it:

  • Enter the key "index.name" in the application.properties file loaded " <context:property-placeholder location="classpath:application.properties" /> "

  • Create a Bean named ConfigBean annotated with @Named or @Component

 @Named public class ConfigBean { @Value("${index.name}") private String indexName; public String getIndexName() { return indexName; } public void setIndexName(String indexName) { this.indexName = indexName; } } 
  • Add configBean.getIndexName () to the @Document annotation using Spring EL: @Document(indexName = "#{ configBean.indexName }", type = "myType")

PS: You can get the same result directly using the implicit Bean "systemProperties" (something like # {systemProperties ['index.name']}), but it didnโ€™t work for me and itโ€™s rather difficult to debug, t solve systemProperties in programmatic context ( https://jira.spring.io/browse/SPR-6651 )

+1
source

Source: https://habr.com/ru/post/1233480/


All Articles