英文:
Error creating bean with elasticSearch and SpringBoot
问题
我想将我的项目连接到Elasticsearch。我遇到了以下错误:
com.example.demo.elasticsearch.controller.Controller`中的字段repository需要一个类型为'com.example.demo.elasticsearch.repository.CustomerRepository'的bean,但找不到。
注入点具有以下注解:
- `@org.springframework.beans.factory.annotation.Autowired(required=true)`
操作:
考虑在配置中定义一个类型为'com.example.demo.elasticsearch.repository.CustomerRepository'的bean。
2020-07-29 15:43:44.525 WARN 14432 --- [ main] o.s.boot.SpringApplication : 无法关闭应用上下文
org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源[org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]中定义的名称为'springApplicationAdminRegistrar'的bean的创建错误:通过方法'springApplicationAdminRegistrar'参数1表达的不满足的依赖项;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有符合类型'org.springframework.core.env.Environment'的bean可用:预期至少有1个符合自动装配候选的bean。依赖注释:{}
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"
}
希望这些信息对你有所帮助。
英文:
I want to connect my project to elastic-search. I am getting the following error:
Field repository in com.example.demo.elasticsearch.controller.Controller required a bean of type 'com.example.demo.elasticsearch.repository.CustomerRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
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]
so I built some classes like the following:
> 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"
}
答案1
得分: 3
你是否有用于创建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版本太旧了)。
英文:
Do you have any configuration class for bean creation like following
@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());
}
}
If not, please try like this.
And also check the version of spring-data-elasticsearch you are using(1.0.0.RELEASE is too old i think)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论