当前位置: 首页 > 面试题库 >

无法将自定义自动配置与spring-data-elasticsearch一起使用

凤财
2023-03-14
问题内容

我无法让spring-data-elasticsearch适用于Elasticsearch 2.0+版本。

关于使用以下POM

<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.project.treasure</groupId>
    <artifactId>search</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <inceptionYear>2015</inceptionYear>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.project.treasure.search.application.Application</start-class>
        <java.version>1.8</java.version>
        <elasticsearch.version>2.2.0</elasticsearch.version>
        <springdata.commons>1.12.0.BUILD-SNAPSHOT</springdata.commons>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.project.treasure.search</groupId>
            <artifactId>search-client</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- Json format -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3</version>
        </dependency>
        <!-- Kafka dependencies -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.10</artifactId>
            <version>0.8.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.9.0.0</version>
        </dependency>
    </dependencies>
        <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>conf/*.properties</exclude>
                </excludes>
            </resource>
        </resources>
        <finalName>ROOT</finalName>
    </build>
</project>

并添加自定义的ElasticsearchConfiguration

package com.package.project.module.application;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.net.InetSocketAddress;

import javax.annotation.Resource;

@Configuration
@PropertySources({
        @PropertySource(value = "classpath:elasticsearch.properties", ignoreResourceNotFound = true),
        @PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = true) })
@EnableElasticsearchRepositories(basePackages = "com.package.project.module.repository")
public class ElasticsearchConfiguration {
    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        Settings settings = Settings.settingsBuilder().put("cluster.name", environment.getProperty("cluster.name")).build();
        Client client = TransportClient.builder().settings(settings).build();
        String[] addresses = environment.getProperty("cluster.nodes").split(",");
        for (int i = 0; i < addresses.length; i++) {
            System.out.println(addresses[i].trim());
            TransportAddress address = new InetSocketTransportAddress(new InetSocketAddress(addresses[i].trim(), Integer.parseInt(environment.getProperty("elasticsearch.port"))));
            ((TransportClient) client).addTransportAddress(address);
        }
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

Application.java* 下面 *

@Configuration
@ComponentScan(basePackages = "com.package.project.module")
@EnableAutoConfiguration(exclude = {ElasticsearchConfiguration.class})
public class Application extends SpringBootServletInitializer {

在有效的POM中,我仍然收到elasticsearch 1.5.2(以及附加的2.0.1.RELEASE版本)作为依赖项。

在运行应用程序时收到以下错误。

java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration.elasticsearchClient
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:178)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:140)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
at com.snapdeal.treasure.search.application.Application.main(Application.java:20)
Caused by: java.lang.IllegalArgumentException: @ConditionalOnMissingBean annotations must specify at least one bean (type, name or annotation)
at org.springframework.util.Assert.isTrue(Assert.java:68)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.validate(OnBeanCondition.java:279)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.<init>(OnBeanCondition.java:275)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:111)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
... 17 common frames omitted

我正在按照spring-data-elasticsearch github repo和docs中提到的说明进行操作。


问题答案:

问题是spring-boot1.3.2.RELEASE尚未与spring-data-elasticsearch2.0.0-RELEASE一起使用。

spring-boot1.3.2将包含spring-boot-starter-data- elasticsearch1.3.2-RELEASE,该版本仍取决于elasticsearch
1.5.2

spring-boot1.4.0-RELEASE版本开始支持ES 2.0 (尚未发布)(但您可以尝试使用1.4.0.BUILD-
SNAPSHOT来查看是否有帮助)

由于您还明确包含spring-data- elasticsearch2.0.0-RELEASE,因此获得了包含2.0版的elasticsearch,但是您应删除该依赖项,因为它是由其spring- boot-starters自己管理的



 类似资料:
  • 与以下属性相关联,但它并没有更好地工作 我该怎么办?

  • 我正在将我的Spring应用程序从Spring-boot 1.5.9迁移到Spring-boot 2.0.0。使用这个新的Spring包,我在Redis中缓存数据时遇到了一些问题。 在我的配置中,我有3个具有不同TTL(长、中、短)的CacheManager: 我还有一个自定义RestTemplate: 在上一个Spring版本中,缓存的每个数据都使用这个RestTemplate,并使用Gener

  • 我无法确定问题的原因,我不知道是分析器无效还是elasticsearch甚至找到了autocomplete-analyser.json文件。我该怎么解决这个?

  • 问题内容: 我知道Android 很棒。它使我们能够播放本地文件以及媒体流。而且非常容易使用(例如): 通过调用具有不同参数集的重载,可以设置不同类型的DataSource 。这个函数有一个有趣的原型: 看起来可以用自己的实现完全覆盖。它确实有效: 并在主要代码中: 是的,这很好。但是,如果我尝试音频/ aacp广播流(例如:“ http://111.223.51.8:8005”-它是“ COOL

  • 我试图通过定义从WebSecurityConfigureAdapter扩展而来的配置类,为我的自定义安全配置(LDAP JWT)创建自己的spring启动程序。但是,当我使用此初学者启动应用程序时,我会得到: 我发现由于Spring Security性的这个问题,不再可能这样做了。Spring的WebSecurityConfiguration中有一个断言: 我解决了这个问题,在启动器中添加了以下内

  • 我很难用Filebeat的Docker自动发现功能来使用定制的Elasticsearch摄取管道。我已经在我的< code>filebeat.yml文件中开始使用自定义处理器,但是我更愿意将它转移到我创建的自定义摄取管道。 首先,这是我使用自定义处理器的配置,它为我的Servarr应用Docker容器提供自定义的类似grok的处理(通过在我的docker-compose.yml文件中对它们应用标签