使用elasticSearch和SpringBoot创建bean时出错



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

com.example.demo.elasticsearch.controller.controller中的字段存储库需要一个类型为"com.example.demo.aelasticsearch.reportory.CustomerRepository"的bean,但找不到该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);
}
}

Customer.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);
}

Build.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"
}

您是否有任何用于创建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数据弹性搜索的版本(1.0.0.RELEASE我认为太旧了(

相关内容

最新更新