聊聊springboot elasticsearch autoconfigure

本文主要研究一下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 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> getIdentifyingAnnotations() {
        return Collections.> singleton(Document.class);
    }

    /*
    * (non-Javadoc)
    * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingTypes()
    */
    @Override
    protected Collection> getIdentifyingTypes() {
        return Arrays.> 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 extends PagingAndSortingRepository {

}
  • ElasticsearchCrudRepository接口继承自PagingAndSortingRepository

ElasticsearchRepository

spring-data-elasticsearch-3.1.6.RELEASE-sources.jar!/org/springframework/data/elasticsearch/repository/ElasticsearchRepository.java

@NoRepositoryBean
public interface ElasticsearchRepository extends ElasticsearchCrudRepository {

     S index(S entity);

    Iterable search(QueryBuilder query);

    Page search(QueryBuilder query, Pageable pageable);

    Page search(SearchQuery searchQuery);

    Page searchSimilar(T entity, String[] fields, Pageable pageable);

    void refresh();

    Class 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

你可能感兴趣的:(springboot)