记录一下,以后也许用的到
spring-servlet.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.hz.*" />
<!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
<mvc:annotation-driven />
<!-- 访问静态资源 -->
<mvc:default-servlet-handler />
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--读取配置文件的参数 -->
<context:property-placeholder location="/WEB-INF/elasticsearch.properties"/>
</beans>
elasticsearch.properties文件
#es集群IP用,分割
elasticsearch.ip=192.168.3.125
#索引
elasticsearch.index=index1
#类型
elasticsearch.type=type1
#集群名称
elasticsearch.clusterName=my-cluster1
java类对象
package com.hz.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConfigInfo {
@Value("${elasticsearch.ip}")
private String ip;
@Value("${elasticsearch.index}")
private String index;
@Value("${elasticsearch.type}")
private String type;
@Value("${elasticsearch.clusterName}")
private String clusterName;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
}
java获得对象的文件
package com.hz.contral;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.core.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
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.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hz.entity.Esentity;
import com.hz.entity.SoGoEntity;
import com.hz.entity.SoGoEntity;
import com.hz.entity.TrackBean;
import com.hz.service.ElasticsearchService;
import com.hz.service.HbaseService;
import com.hz.util.ConfigInfo;
import com.hz.util.PageUtils;
@Controller
public class ElasticsearchContral {
@Autowired
private ConfigInfo configInfo;
//将查询值返回
@RequestMapping("/searchPageJson")
public void searchPageJson(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException {
String index=configInfo.getIp();
String index2=configInfo.getIndex();
System.out.println(index);
System.out.println(index2);
}
}
就可以读取到信息了