创建名为'client'的bean时出错

时间:2017-04-17 09:07:25

标签: java spring maven elasticsearch spring-boot

我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*错误 创建名为'client'的bean *异常,不确定出了什么问题 因为我以前找不到任何类似的路径...... 非常感谢指导人们。 这是mu elasticsearch配置:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.google.gms.google-services'
android {
  compileSdkVersion 25
  buildToolsVersion "25.0.2"
  defaultConfig {
    applicationId "com.york.yorkbbs"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 531
    versionName "5.31"
    multiDexEnabled true
    ndk {
        abiFilters "armeabi", "armeabi-v7a"
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
dexOptions {
    preDexLibraries = true
    javaMaxHeapSize "4g"
}

lintOptions{
    checkReleaseBuilds false
    abortOnError false
}

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
}
}

repositories {
   mavenCentral()
}

dependencies {
  compile fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:multidex:1.0.1'
  compile project(':library_googlemap')
  compile project(':library_jcvideoplayer')
  compile 'com.android.support:recyclerview-v7:25.3.0'
  compile 'com.google.code.gson:gson:2.7'
  compile 'com.google.firebase:firebase-core:10.2.1'
  compile 'com.google.android.gms:play-services-tagmanager:10.2.1'
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
  compile 'com.stripe:stripe-android:3.1.0'
  compile 'com.paypal.sdk:paypal-android-sdk:2.15.3'//exclude group: 'com.squareup.okhttp3', module:'okhttp'
  compile 'com.facebook.fresco:fresco:0.14.1'
  compile 'com.facebook.fresco:animated-gif:0.14.1'
  compile 'io.reactivex:rxandroid:1.2.1'
  compile 'io.reactivex:rxjava:1.2.1'
  compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:1.0.2'
  compile 'com.facebook.android:facebook-android-sdk:4.+'
  compile 'com.amazonaws:aws-android-sdk-s3:2.4.1'
  compile project(':Qupaisdk-release')
  compile 'com.google.code.findbugs:jsr305:3.0.0'
  compile 'com.fasterxml.jackson.core:jackson-databind:2.6.4'
  compile 'com.google.dagger:dagger-compiler:2.0.1'
  compile 'com.google.dagger:dagger:2.0.1'
  compile 'jp.wasabeef:blurry:2.1.1'
 }

这是我的主要申请:

repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.0'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3.1'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    classpath 'com.google.gms:google-services:3.0.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}


allprojects {
   repositories {
    jcenter()
}

我的pom.xml:

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.backend.project.repositories")
public class EsConfig {


@Bean
public Client client() throws Exception{
    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("elasticsearch.host"), 9200));    
    return client;
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}}

这是异常追踪:

import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;

import com.backend.project.models.Book;
import com.backend.project.services.BookService;

import java.util.Map;
@Configuration
@ComponentScan(basePackages = { "com.backend.project" })
@EnableAutoConfiguration
@SpringBootApplication

public class WebApplication implements CommandLineRunner {

@Autowired
private ElasticsearchOperations es;

@Autowired
private BookService bookService;

public static void main(String args[]) {
    SpringApplication.run(WebApplication.class, args);
}

@Override
public void run(String... args) throws Exception {

    printElasticSearchInfo();

    bookService.save(new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017"));
    bookService.save(new Book("1002", "Apache Lucene Basics", "Rambabu Posa", "13-MAR-2017"));
    bookService.save(new Book("1003", "Apache Solr Basics", "Rambabu Posa", "21-MAR-2017"));

    //fuzzey search
    Page<Book> books = bookService.findByAuthor("Rambabu", new PageRequest(0, 10));

    //List<Book> books = bookService.findByTitle("Elasticsearch Basics");

    books.forEach(x -> System.out.println(x));


}

//useful for debug, print elastic search details
private void printElasticSearchInfo() {

    System.out.println("--ElasticSearch--");
    Client client = es.getClient();
    Map<String, String> asMap = client.settings().getAsMap();

    asMap.forEach((k, v) -> {
        System.out.println(k + " = " + v);
    });
    System.out.println("--ElasticSearch--");
}}

2 个答案:

答案 0 :(得分:0)

除了添加elasticsearch客户端依赖项之外,还需要添加elasticsearch依赖项:

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.4.0</version>
    </dependency>

对于你的依赖;确保:

  1. 您在计算机上运行的是与pom.xml中所述相同的ES版本
  2. 您运行的ES版本和弹簧数据与您正在使用的Spring Boot App版本兼容。
  3. AFAIK Spring Boot 1.5.2与ES 5.x或6.x不兼容所以我会考虑使用以前版本的ES或将Spring Boot升级到支持ES 5.x和6的2.0.0。 X

答案 1 :(得分:0)

在 pom.xml 中添加来自 elasticsearch 的最新版本 你可以这样使用

如果使用这个依赖

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
    </dependency>

或者这样,如果使用这个依赖:

    <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    </dependency>

或同时使用。