spring读取properties的方法

4 篇文章 0 订阅

记录一下,以后也许用的到

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);
		
		 
	}
	

	 

}
就可以读取到信息了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值