Talk about spring bootelastic search autoconfigure

  springboot

Order

This article mainly studies the spring bootelastic search 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 created 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 {

}
  • ElasticeSearchRepositorIesautoConfiguration mainly imports ElasticeSearchRepositorIesregistrar.

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 {

    }

}
  • The ElasticeSearchRepositoryRepositoryConfigurationExtension method is covered here, returning the ElasticeSearchRepositoryConfigurationExtension.

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);
    }
}
  • The ElasticeSearchRepositoryConfigExtension covers the getIdentifyingTypes method and returns ElasticeSearchCRUDERPOSITORY.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> {

}
  • The ElasticsearchCrudRepository interface inherits from 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();
}
  • The ElasticsearchRepository inherits the ElasticsearchCrudRepository and supports the index, search, searchSimilar, refresh, getEntityClass methods.

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();
    }

}
  • ElasticeSearchDataAutoConfiguration mainly created ElasticsearchTemplate; ; SimpleElasticeSearchMappingContext was created to create an ElasticeSearchConverter, while ElasticeSearchConverter was created because the ElasticsearchTemplate requires an ElasticeSearchConverter.

Summary

  • Spring-boot-auto configuration module provides elasticsearch with three autoconfiguration, namely, ElasticsearchAutoConfiguration, elasticsearchdepositoriesoutoconfiguration, and elasticsearchdataautoconfiguration.
  • ElasticsearchAutoConfiguration created TransportClient; ; ELASTICS RESEARCH REPOSITORIESATOCONFIGuration mainly imports ELASTICS RESEARCH REPOSITORIESREGISTRAR to support the annotation of @ ENABLE ELASTICS RESEARCH REPOSITORIES
  • ElasticeSearchDataAutoConfiguration mainly created ElasticsearchTemplate; ; SimpleElasticeSearchMappingContext was created to create an ElasticeSearchConverter, while ElasticeSearchConverter was created because the ElasticsearchTemplate requires an ElasticeSearchConverter.

doc