spring-es.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd "><context:component-scan base-package="com.es.esClientFactory" /><bean id="esConf" class="org.es.conf.EsConf"><constructor-arg value="master,slave,slave2" /><constructor-arg value="elasticsearch" /><constructor-arg value="9300" /> </bean> </beans>

EsClientFactory.java

package com.es.esClientFactory;import java.net.InetAddress;
import java.net.UnknownHostException;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.elasticsearch.transport.client.PreBuiltTransportClient;
import org.es.conf.EsConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class EsClientFactory {@Autowiredprivate EsConf esConf;@Bean(name = "esClient")public Client getESClient() {Settings settings = Settings.builder().put("cluster.name", esConf.getClusterName()).build();Client client = new PreBuiltTransportClient(settings);for (String ip : esConf.getIps().split(",")) {try {((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), 9300));} catch (UnknownHostException e) {e.printStackTrace();}}return client;}}

EsServiceImpl.java

package org.taian.service.impl;import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;@Service
public class EsServiceImpl {@Autowired @Qualifier("esClient")private Client client;public GetResponse testes(){GetResponse response = client.prepareGet("test", "t5", "285309").execute().actionGet();System.out.println(response.toString());System.out.println("configuration");System.out.println(response.getSourceAsString());return null;}
}

ESContoller.java

package org.taian.web;import java.util.Date;import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.taian.service.ElasticsearchService;
import org.taian.service.impl.EsServiceImpl;
import org.taian.utils.HttpRequestUtil;
import org.taian.utils.JSONUtils;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;@Controller
@RequestMapping("/myes")
public class ESController {private final Logger logger = LoggerFactory.getLogger(this.getClass());@AutowiredEsServiceImpl estest;@ResponseBody@RequestMapping(value="testes", method = { RequestMethod.GET, RequestMethod.POST }, produces="application/json")public JSONObject testes(){estest.testes();return JSON.parseObject("");}
}

spring 通过EsClientFactory注入elasticsearch相关推荐

  1. 万字长文:详解 Spring Boot 中操作 ElasticSearch

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 超级小豆丁 来源 | http://www.m ...

  2. Spring——Filter过滤器注入Bean时注入失败[NULL]

    问题描述 Spring中Filter注入Bean时注入失败,Bean一直为空. @Slf4j @Component public class RestAuthFilter extends FormAu ...

  3. Spring 多线程下注入bean问题

    问题 Spring中多线程注入userThreadService注不进去,显示userThreadService为null异常 代码如下: public class UserThreadTask im ...

  4. factorybean 代理类不能按照类型注入_《Spring入门经典》:使用Spring进行依赖注入

    第二章:使用Spring进行依赖注入 重点:配置并使用Spring容器 使用不同类型的配置元数据来配置Spring容器 理解依赖解析 了解自动装配的优缺点 在容器中执行显式Bean查找 学习不同的Be ...

  5. 在ABAP里模拟实现Java Spring的依赖注入

    Dependency Injection- 依赖注入,在Java Spring框架中有着广泛地应用.通过依赖注入,我们不必在应用代码里繁琐地初始化依赖的资源,非常方便. 那么ABAP能否从语言层面上也 ...

  6. Spring Setter依赖注入示例

    学习如何编写Spring Setter依赖注入示例 . Setter注入是Spring依赖注入的一种 . Spring支持字段注入,Setter注入以及构造函数注入,以将依赖项注入Spring托管的b ...

  7. Spring字段依赖注入示例

    学习如何编写Spring Field Injection示例 . 字段注入是Spring框架 依赖注入的一种 . 在本教程中,我们将编写几个类,并看一看现场注入工程. 有关Spring依赖注入的更多信 ...

  8. Spring构造函数依赖注入示例

    欢迎使用Spring构造函数依赖注入示例指南. 基于构造器的依赖注入是Spring 依赖注入的一种 . 依赖注入的另一种类型是Setter注入和字段注入. 有关Spring依赖注入的更多信息: Spr ...

  9. spring三种注入方式

    设置Spring的作用域 或者使用枚举值设置 单例和多里使用场景 自动注入 @Primary 一个接口有多个实现被spring管理吗,在依赖注入式,spring会不知道注入哪个实现类就会抛出NoUni ...

最新文章

  1. 共谋大数据产业发展新篇章
  2. 阿里云ECS使用cloudfs4oss挂载OSS
  3. 预训练永不止步,游戏问答语言模型实操
  4. thinkphp3.23开发的“二当家的”官网
  5. .NET应用程序7种最常见的性能问题及其解决方案
  6. el-select 结合 el-checkBox 实现下拉全选+多选功能;el-select下拉框全选功能;
  7. 关于java中锁的面试题_Java面试题-Java中的锁
  8. Java案例:静态内部类
  9. C++编译器与链接器工作原理
  10. java 怎样展示二维画矩阵图_如何用java绘制矩阵的图
  11. 第2.01节 发布版本vs源码编译
  12. 《大般涅槃经》略释 净慧法师
  13. 【云原生|实践指北】5:真实业务场景下云原生项目落地实践学习
  14. 树莓派耳机接口有电流声、杂音
  15. php sesstion,操作Session的PHP类
  16. 什么是Web服务器,如何配置
  17. 做大做强并非国企改革唯一选项
  18. 电磁波以及常见电磁波波谱
  19. 2023年PMP新考纲,考生必知(含备考资料)
  20. 微软模拟飞行2020服务器多少内存,微软飞行模拟2020配置需求高吗 微软飞行模拟2020配置要求-游侠网...

热门文章

  1. 数据结构与算法(C语言) | 栈和队列——栈(自己做过测试)
  2. PHP的替换strstr strtr str_replace substr_replace
  3. 反思各种型格人做事方法
  4. Linux的make 命令出现:make:*** No targets specified and no makefile found.Stop
  5. Mysql的date_format与date_sub
  6. Linux的Nginx七:对比|模块
  7. x86架构和arm架构_RISC-V架构1000核CPU登场 x86架构腹背受敌
  8. 华为在哪发布的鸿蒙,华为鸿蒙2.0可以替代安卓吗,华为鸿蒙2.0优势在哪
  9. PHP中一些可用的方法
  10. python成员方法共享吗_python 入坑路 类的特殊成员方法