【问题标题】:Error creating bean with elasticSearch and SpringBoot使用 elasticSearch 和 Spring Boot 创建 bean 时出错
【发布时间】:2020-11-19 03:37:55
【问题描述】:

我想将我的项目连接到弹性搜索。我收到以下错误:

com.example.demo.elasticsearch.controller.Controller 中的字段存储库需要一个无法找到的“com.example.demo.elasticsearch.repository.CustomerRepository”类型的 bean。

注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

Consider defining a bean of type 'com.example.demo.elasticsearch.repository.CustomerRepository' in your configuration.

2020-07-29 15:43:44.525  WARN 14432 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Unsatisfied dependency expressed through method 'springApplicationAdminRegistrar' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]

所以我构建了一些类,如下所示:

Controller.java

package com.example.demo.elasticsearch.controller;

import com.example.demo.elasticsearch.model.Customer;
import com.example.demo.elasticsearch.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@org.springframework.stereotype.Controller
public class Controller {

    @Autowired
    private CustomerRepository repository;

    @PostMapping("/saveCustomer")
    public int saveCustomer(@RequestBody List<Customer> customers) {
        repository.saveAll(customers);
        return customers.size();
    }

    @GetMapping("/findAll")
    public Iterable<Customer> findAllCustomers() {
        return repository.findAll();
    }

    @GetMapping("/findByFName/{firstName}")
    public List<Customer> findByFirstName(@PathVariable String firstName) {
        return repository.findByFirstname(firstName);
    }
}

客户.java

package com.example.demo.elasticsearch.model;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "javatechie", type = "customer", shards = 2)
@Data
@AllArgsConstructor
@NoArgsConstructor

public class Customer {

    @Id
    private String id;
    private String firstname;
    private String lastname;
    private int age;

}

CustomerRepository.java

package com.example.demo.elasticsearch.repository;


import com.example.demo.elasticsearch.model.Customer;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {

    List<Customer> findByFirstname(String firstName);

}

构建.gradle

// https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch
    compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '1.0.0.RELEASE'

 dependencies {
        classpath "org.elasticsearch.gradle:build-tools:6.5.4"
    }

【问题讨论】:

  • 您好,该错误与您尝试在 SpringApplicationAdminJmxAutoConfiguration.class 中自动装配的“springApplicationAdminRegistrar”有关,您能告诉我 SpringApplicationAdminJmxAutoConfiguration.class 和与 springApplicationAdminRegistrar 相关的类吗?
  • 我不知道这门课是干什么用的。我根本不使用它,也许这是来自编译器的??
  • 尝试使用“springApplicationAdminRegistrar”查找你在哪里
  • 查看更新帖
  • 尝试在SpringApplicationAdminMXBeanRegistrar类中添加@Component注解

标签: java spring-boot elasticsearch gradle


【解决方案1】:

你有没有像下面这样的 bean 创建配置类

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.elasticsearch.repository")
public class Config {
 
    @Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration 
            = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
 
        return RestClients.create(clientConfiguration).rest();
    }
 
    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}

如果没有,请尝试这样。 并检查您正在使用的 spring-data-elasticsearch 的版本(我认为 1.0.0.RELEASE 太旧了)

【讨论】:

  • 根据 maven.org 于 2014 年发布
  • 我有另一个错误,如果我使用 repository.saveAll(customers) 来存储数据,我会收到以下错误:Elasticsearch exception [type=illegal_argument_exception, 知道为什么吗?
  • 您能否提供该异常的完整日志@Catalina
  • Servlet.service() for servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch 异常 [type=action_request_validation_exception, reason=Validation Failed: 1: type is missing;];嵌套异常是 ElasticsearchStatusException[Elasticsearch 异常 [type=action_request_validation_exception, reason=Validation Failed: 1: type is missing;]]] 的根本原因
猜你喜欢
  • 2017-02-03
  • 1970-01-01
  • 2017-04-17
  • 2015-07-23
  • 2023-02-09
  • 1970-01-01
  • 2015-02-25
  • 2019-02-12
  • 2020-06-05
相关资源
最近更新 更多