Make ElasticSearchAutoConfiguration conditional on SD Elasticsearch
ElasticSearchAutoConfiguration depends on two Spring Data Elasticsearch classes (TransportClientFactoryBean and NodeClientFactoryBean), however it’s only conditional on Elasticsearch itself being on the classpath. This lead to start up failures due to a ClassNotFoundException. Its @ConditionalOnClass configuration has been updated so that the auto-configuration will only be enabled if both Elasticsearch and Spring Data Elasticsearch are on the classpath. The dependencies on TransportClientFactoryBean and NodeClientFactoryBean were ‘hidden’ in ElasticsearchProperties. The logic that uses these types has been moved into ElasticSearchAutoConfiguration so that the usage of the types and the related @ConditionalOnClass configuration is in the same file. Fixes #1023
This commit is contained in:
parent
9b44c322d4
commit
f7d1aab9f3
|
|
@ -25,17 +25,22 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
* Auto-configuration} for Elasticsearch.
|
||||
*
|
||||
*
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(Client.class)
|
||||
@ConditionalOnClass({ Client.class, TransportClientFactoryBean.class,
|
||||
NodeClientFactoryBean.class })
|
||||
@EnableConfigurationProperties(ElasticsearchProperties.class)
|
||||
public class ElasticsearchAutoConfiguration implements DisposableBean {
|
||||
|
||||
|
|
@ -48,10 +53,35 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
|
|||
|
||||
@Bean
|
||||
public Client elasticsearchClient() {
|
||||
this.client = this.properties.createClient();
|
||||
try {
|
||||
if (StringUtils.hasLength(this.properties.getClusterNodes())) {
|
||||
this.client = createTransportClient();
|
||||
}
|
||||
else {
|
||||
this.client = createNodeClient();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
return this.client;
|
||||
}
|
||||
|
||||
private Client createNodeClient() throws Exception {
|
||||
NodeClientFactoryBean factory = new NodeClientFactoryBean(true);
|
||||
factory.setClusterName(this.properties.getClusterName());
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
private Client createTransportClient() throws Exception {
|
||||
TransportClientFactoryBean factory = new TransportClientFactoryBean();
|
||||
factory.setClusterName(this.properties.getClusterName());
|
||||
factory.setClusterNodes(this.properties.getClusterNodes());
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
if (this.client != null) {
|
||||
|
|
|
|||
|
|
@ -16,15 +16,11 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.elasticsearch;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Configuration properties for Elasticsearch.
|
||||
*
|
||||
*
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
* @since 1.1.0
|
||||
|
|
@ -36,8 +32,6 @@ public class ElasticsearchProperties {
|
|||
|
||||
private String clusterNodes;
|
||||
|
||||
private boolean local = true;
|
||||
|
||||
public String getClusterName() {
|
||||
return this.clusterName;
|
||||
}
|
||||
|
|
@ -53,30 +47,4 @@ public class ElasticsearchProperties {
|
|||
public void setClusterNodes(String clusterNodes) {
|
||||
this.clusterNodes = clusterNodes;
|
||||
}
|
||||
|
||||
public Client createClient() {
|
||||
try {
|
||||
return (StringUtils.hasLength(this.clusterNodes) ? createTransportClient()
|
||||
: createNodeClient());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Client createNodeClient() throws Exception {
|
||||
NodeClientFactoryBean factory = new NodeClientFactoryBean(this.local);
|
||||
factory.setClusterName(this.clusterName);
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
private Client createTransportClient() throws Exception {
|
||||
TransportClientFactoryBean factory = new TransportClientFactoryBean();
|
||||
factory.setClusterName(this.clusterName);
|
||||
factory.setClusterNodes(this.clusterNodes);
|
||||
factory.afterPropertiesSet();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue