首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在spring-boot升级到2.2.0.RELEASE后,Elasticsearch无法工作

在spring-boot升级到2.2.0.RELEASE后,Elasticsearch无法工作
EN

Stack Overflow用户
提问于 2021-11-10 06:41:26
回答 1查看 73关注 0票数 1

我正在将我们的一个应用程序从spring-boot版本2.1.3.RELEASE升级到2.2.0.RELEASE,但是我遇到了一些Elasticsearch的问题。下面是我得到的错误:

代码语言:javascript
复制
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customCdrElasticConfig': Unsatisfied dependency expressed through field 'elasticsearchTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=elasticsearchTemplate)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=elasticsearchTemplate)}

代码:

代码语言:javascript
复制
package com.codex.reporting.config;

import com.codex.reporting.elasticsearch.service.impl.CustomElasticsearchTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@Configuration
public class CustomCdrElasticConfig {

    @Autowired
    @Qualifier("elasticsearchTemplate")
    private ElasticsearchTemplate elasticsearchTemplate;

    @Bean
    public CustomElasticsearchTemplate customElasticsearchTemplate(){
        return new CustomElasticsearchTemplate(elasticsearchTemplate.getClient());
    }
}

pom.xml

代码语言:javascript
复制
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>6.8.13</version>
        </dependency>
        <!-- Dependency for high level rest client, for Rest Client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>6.8.13</version>
        </dependency>
        <!-- For Rest High Level Client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.8.13</version>
        </dependency>
        <!-- Dependency for high level rest client, for elasticsearch Core -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch-core</artifactId>
            <version>6.8.13</version>
        </dependency>
EN

回答 1

Stack Overflow用户

发布于 2021-11-10 18:42:38

您可能会面临this问题。

我正在尝试测试,并根据您的构建文件对以下pom进行了调整

代码语言:javascript
复制
    <name>demo</name>
    <description>Spring Boot ElasticSearch</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath />
    </parent>

   <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
                <version>3.2.3.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>x-pack-transport</artifactId>
                <version>6.8.13</version>
            </dependency>
            <!-- Dependency for high level rest client, for Rest Client -->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>6.8.13</version>
            </dependency>
            <!-- For Rest High Level Client -->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.8.13</version>
            </dependency>
            <!-- Dependency for high level rest client, for elasticsearch Core -->
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch-core</artifactId>
                <version>6.8.13</version>
            </dependency>
    </dependencies>

我的服务类看起来像这样

代码语言:javascript
复制
@Service
public class TestService {
    
    
    @Autowired
    @Qualifier("elasticsearchTemplate")
    private ElasticsearchTemplate elasticsearchTemplate;
    
    public void ping() {
        System.out.println(elasticsearchTemplate.getClient().settings().names());
        
    }

}

和application.properties中的以下属性

代码语言:javascript
复制
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9200

这样,应用程序可以正常启动,这意味着elasticsearchTemplate可以正确地自动连接

代码语言:javascript
复制
2021-11-11 00:06:03.767  INFO 24304 --- [           main] com.test.elasticsearch.demo.Application  : No active profile set, falling back to default profiles: default
2021-11-11 00:06:06.408  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2021-11-11 00:06:06.445  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 24ms. Found 0 repository interfaces.
2021-11-11 00:06:06.460  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2021-11-11 00:06:06.481  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20ms. Found 0 repository interfaces.
2021-11-11 00:06:08.810  INFO 24304 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-11-11 00:06:08.830  INFO 24304 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-11 00:06:08.831  INFO 24304 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2021-11-11 00:06:09.279  INFO 24304 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-11-11 00:06:09.280  INFO 24304 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5432 ms
2021-11-11 00:06:11.090  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : no modules loaded
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2021-11-11 00:06:14.516  INFO 24304 --- [           main] o.s.d.e.c.TransportClientFactoryBean     : Adding transport node : 127.0.0.1:9200
2021-11-11 00:06:48.707  INFO 24304 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69908841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档