2

I am upgrading one of our applications from spring-boot version 2.1.3.RELEASE to 2.2.0.RELEASE but I am facing some issues with Elasticsearch. Here is the error I am getting:

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

Code:

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

        <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>
lkatiforis
  • 5,703
  • 2
  • 16
  • 35
  • I have checked [here](https://github.com/spring-projects/spring-boot/blob/2.2.x/spring-boot-project/spring-boot-dependencies/pom.xml) on the Spring Boot project POM file and verified that `6.8.13` is the correct version for ElasticSearch when using Spring Boot `2.2.x`. – Tim Biegeleisen Nov 10 '21 at 06:45
  • "ElasticsearchTemplate" (org.springframework.data.elasticsearch.core.ElasticsearchTemplate) is a part of Spring Data Elasticsearch and I don't see that dependency in the pom you have shared. – Shailendra Nov 10 '21 at 11:46
  • @Shailendra : Sorry missed adding that in the question earlier, it is already present in the pom.xml. I am using version 3.2.3.RELEASE. – Simarjit Singh Nov 10 '21 at 13:37
  • Is this resolved? – Saurabh Gadariya Jun 27 '22 at 13:45

1 Answers1

0

You could be facing this issue.

I was trying to test and have below pom - adjusted as per your build file

    <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>

And my service class looks like this

@Service
public class TestService {
    
    
    @Autowired
    @Qualifier("elasticsearchTemplate")
    private ElasticsearchTemplate elasticsearchTemplate;
    
    public void ping() {
        System.out.println(elasticsearchTemplate.getClient().settings().names());
        
    }

}

And following properties in application.properties

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9200

With this - the application starts up fine implying the elasticsearchTemplate gets autowired correctly

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'
Shailendra
  • 8,874
  • 2
  • 28
  • 37