Spring Boot Elasticsearch Configuration
Remove your configuration class and properties.
Add the following dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
Just add the spring.data.elasticsearch
properties to an application-prod.properties
and application-dev.properties
and change for the desired environment. This is described in the ElasticSearch section of the Spring Boot guide.
spring.data.elasticsearch.cluster-nodes=10.10.1.10:9300
The value in either file will of course differ (or put the default in the application.properties
and simply override with an application-dev.properties
.
Spring Boot will based on the spring.profiles.active
load the desired properties file.
There is no need to hack around yourself.
I agree with Deinum, if you are using Spring boot it will get the properties from the active profile active.
I have different profiles in my project and this is my elasticsearch configuration:
@Configurationpublic class ElasticSearchConfiguration { @Value("${spring.data.elasticsearch.cluster-name}") private String clusterName; @Value("${spring.data.elasticsearch.cluster-nodes}") private String clusterNodes; @Bean public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException { String server = clusterNodes.split(":")[0]; Integer port = Integer.parseInt(clusterNodes.split(":")[1]); Settings settings = Settings.settingsBuilder() .put("cluster.name", clusterName).build(); client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(server), port)); return new ElasticsearchTemplate(client); }