创建bean时出错,通过工厂方法实例化Bean失败

时间:2018-09-27 14:09:08

标签: spring-boot elasticsearch javabeans spring-data-elasticsearch

我试图在Spring引导中配置elasticsearch,但是Bean实例化失败,Node Builder已从elasticsearch api中删除,因此我试图使用Settings.Builder,但它没有帮助,将近一个星期都停滞了, 请帮忙,下面是正在使用的代码:

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
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;

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.demo.elastic.elasticdemo.repository")
public class ElasticConfiguration {

@SuppressWarnings("resource")
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws IOException {

    File tempDir = File.createTempFile("temp-elastic", Long.toString(System.nanoTime()));
    System.out.println("Temp directory: "+ tempDir.getAbsolutePath());

    Settings.Builder settings = Settings.builder()

            //http settings
            .put("http.enable", "true")
            .put("http.cor.enable", "true")
            .put("http.cors.allow-origin", "https?:?/?/localhost(:[0-9]+)?/")
            .put("http.port", "9200")

            //transport settings
            .put("transport.tcp.port", "9300")
            .put("network.host", "localhost")

            //node settings
            .put("node.data", "true")
            .put("node.master", "true")

            //configuring index
            .put("index.number_of_shards", "1")
            .put("index.number_of_replicas", "2")
            .put("index.refresh_interval", "10s")
            .put("index.max_results_window", "100")

            //configuring paths
            .put("path.logs", new File (tempDir, "logs").getAbsolutePath())
            .put("path.data", new File (tempDir, "data").getAbsolutePath())
            .put("path.home", tempDir);
            //.build();

    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));

    return new ElasticsearchTemplate(client);
}
}

我在做什么错??

错误越来越严重:

2018-09-27 19:23:35.825错误57876 --- [main] o.s.boot.SpringApplication:应用程序运行失败

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ loaders”的bean时出错:通过字段“ operations”表示不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[com / demo / elastic / elasticdemo / config / ElasticConfiguration.class]中定义的名称为'elasticsearchTemplate'的bean时出错:通过工厂方法的Bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.elasticsearch.core.ElasticsearchOperations]:工厂方法'elasticsearchTemplate'抛出了异常;嵌套的异常是java.lang.NoClassDefFoundError:org / elasticsearch / transport / Netty3Plugin     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)〜[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]中     在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)〜[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]

0 个答案:

没有答案