序
本文主要研究一下 springboot elasticsearch autoconfigure
ElasticsearchAutoConfiguration
spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.java
@Configuration
@ConditionalOnClass({Client.class, TransportClientFactoryBean.class})
@ConditionalOnProperty(prefix = “spring.data.elasticsearch”, name = “cluster-nodes”,
matchIfMissing = false)
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchAutoConfiguration {
private final ElasticsearchProperties properties;
public ElasticsearchAutoConfiguration(ElasticsearchProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public TransportClient elasticsearchClient() throws Exception {
TransportClientFactoryBean factory = new TransportClientFactoryBean();
factory.setClusterNodes(this.properties.getClusterNodes());
factory.setProperties(createProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
private Properties createProperties() {
Properties properties = new Properties();
properties.put(“cluster.name”, this.properties.getClusterName());
properties.putAll(this.properties.getProperties());
return properties;
}
}
ElasticsearchAutoConfiguration 创建了 TransportClient
ElasticsearchRepositoriesAutoConfiguration
spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchRepositoriesAutoConfiguration.java
@Configuration
@ConditionalOnClass({Client.class, ElasticsearchRepository.class})
@ConditionalOnProperty(prefix = “spring.data.elasticsearch.repositories”,
name = “enabled”, havingValue = “true”, matchIfMissing = true)
@ConditionalOnMissingBean(ElasticsearchRepositoryFactoryBean.class)
@Import(ElasticsearchRepositoriesRegistrar.class)
public class ElasticsearchRepositoriesAutoConfiguration {
}
ElasticsearchRepositoriesAutoConfiguration 主要是导入了 ElasticsearchRepositoriesRegistrar
ElasticsearchRepositoriesRegistrar
spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchRepositoriesRegistrar.java
class ElasticsearchRepositoriesRegistrar
extends AbstractRepositoryConfigurationSourceSupport {
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableElasticsearchRepositories.class;
}
@Override
protected Class<?> getConfiguration() {
return EnableElasticsearchRepositoriesConfiguration.class;
}
@Override
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
return new ElasticsearchRepositoryConfigExtension();
}
@EnableElasticsearchRepositories
private static class EnableElasticsearchRepositoriesConfiguration {
}
}
ElasticsearchRepositoriesRegistrar 这里覆盖了 getRepositoryConfigurationExtension 方法,返回 ElasticsearchRepositoryConfigExtension
ElasticsearchRepositoryConfigExtension
spring-data-elasticsearch-3.1.6.RELEASE-sources.jar!/org/springframework/data/elasticsearch/repository/config/ElasticsearchRepositoryConfigExtension.java
public class ElasticsearchRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getRepositoryFactoryBeanClassName()
*/
@Override
public String getRepositoryFactoryBeanClassName() {
return ElasticsearchRepositoryFactoryBean.class.getName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
*/
@Override
protected String getModulePrefix() {
return “elasticsearch”;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
AnnotationAttributes attributes = config.getAttributes();
builder.addPropertyReference(“elasticsearchOperations”, attributes.getString(“elasticsearchTemplateRef”));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.XmlRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
Element element = config.getElement();
builder.addPropertyReference(“elasticsearchOperations”, element.getAttribute(“elasticsearch-template-ref”));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingAnnotations()
*/
@Override
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
return Collections.<Class<? extends Annotation>> singleton(Document.class);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingTypes()
*/
@Override
protected Collection<Class<?>> getIdentifyingTypes() {
return Arrays.<Class<?>> asList(ElasticsearchRepository.class, ElasticsearchCrudRepository.class);
}
}
ElasticsearchRepositoryConfigExtension 覆盖了 getIdentifyingTypes 方法,返回的是 ElasticsearchCrudRepository.class、ElasticsearchRepository.class
ElasticsearchCrudRepository
spring-data-elasticsearch-3.1.6.RELEASE-sources.jar!/org/springframework/data/elasticsearch/repository/ElasticsearchCrudRepository.java
@NoRepositoryBean
public interface ElasticsearchCrudRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
}
ElasticsearchCrudRepository 接口继承自 PagingAndSortingRepository
ElasticsearchRepository
spring-data-elasticsearch-3.1.6.RELEASE-sources.jar!/org/springframework/data/elasticsearch/repository/ElasticsearchRepository.java
@NoRepositoryBean
public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {
<S extends T> S index(S entity);
Iterable<T> search(QueryBuilder query);
Page<T> search(QueryBuilder query, Pageable pageable);
Page<T> search(SearchQuery searchQuery);
Page<T> searchSimilar(T entity, String[] fields, Pageable pageable);
void refresh();
Class<T> getEntityClass();
}
ElasticsearchRepository 继承了 ElasticsearchCrudRepository,支持了 index、search、searchSimilar、refresh、getEntityClass 方法
ElasticsearchDataAutoConfiguration
spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.java
@Configuration
@ConditionalOnClass({Client.class, ElasticsearchTemplate.class})
@AutoConfigureAfter(ElasticsearchAutoConfiguration.class)
public class ElasticsearchDataAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(Client.class)
public ElasticsearchTemplate elasticsearchTemplate(Client client,
ElasticsearchConverter converter) {
try {
return new ElasticsearchTemplate(client, converter);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@Bean
@ConditionalOnMissingBean
public ElasticsearchConverter elasticsearchConverter(
SimpleElasticsearchMappingContext mappingContext) {
return new MappingElasticsearchConverter(mappingContext);
}
@Bean
@ConditionalOnMissingBean
public SimpleElasticsearchMappingContext mappingContext() {
return new SimpleElasticsearchMappingContext();
}
}
ElasticsearchDataAutoConfiguration 主要创建了 ElasticsearchTemplate;创建 SimpleElasticsearchMappingContext 是为了创建 ElasticsearchConverter,而创建 ElasticsearchConverter 是因为创建 ElasticsearchTemplate 需要 ElasticsearchConverter
小结
spring-boot-autoconfigure module 给 elasticsearch 提供了三个 auto configuration,分别是 ElasticsearchAutoConfiguration、ElasticsearchRepositoriesAutoConfiguration、ElasticsearchDataAutoConfiguration
ElasticsearchAutoConfiguration 创建了 TransportClient;ElasticsearchRepositoriesAutoConfiguration 主要是导入了 ElasticsearchRepositoriesRegistrar,给 @EnableElasticsearchRepositories 注解提供支持
ElasticsearchDataAutoConfiguration 主要创建了 ElasticsearchTemplate;创建 SimpleElasticsearchMappingContext 是为了创建 ElasticsearchConverter,而创建 ElasticsearchConverter 是因为创建 ElasticsearchTemplate 需要 ElasticsearchConverter
doc
spring-boot-autoconfigure module