创建弹性搜索和SpringBoot的Bean时出错。

huangapple go评论64阅读模式
英文:

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 &#39;com.example.demo.elasticsearch.repository.CustomerRepository&#39; 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 &#39;springApplicationAdminRegistrar&#39; defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Unsatisfied dependency expressed through method &#39;springApplicationAdminRegistrar&#39; parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type &#39;org.springframework.core.env.Environment&#39; 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(&quot;/saveCustomer&quot;)
    public int saveCustomer(@RequestBody List&lt;Customer&gt; customers) {
        repository.saveAll(customers);
        return customers.size();
    }

    @GetMapping(&quot;/findAll&quot;)
    public Iterable&lt;Customer&gt; findAllCustomers() {
        return repository.findAll();
    }

    @GetMapping(&quot;/findByFName/{firstName}&quot;)
    public List&lt;Customer&gt; 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 = &quot;javatechie&quot;, type = &quot;customer&quot;, 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&lt;Customer, String&gt; {

    List&lt;Customer&gt; findByFirstname(String firstName);

}

> Build.gradle

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

 dependencies {
        classpath &quot;org.elasticsearch.gradle:build-tools:6.5.4&quot;
    }

答案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 = &quot;com.example.demo.elasticsearch.repository&quot;)
public class Config {
 
    @Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration 
            = ClientConfiguration.builder()
                .connectedTo(&quot;localhost:9200&quot;)
                .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)

huangapple
  • 本文由 发表于 2020年7月29日 22:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63155425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定