读取后台配置文件

创建PropertieUtil.java

package org.test.base.common;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class PropertieUtil {
	private static Logger logger = Logger.getLogger(PropertieUtil.class);
	private static Map<String, Object> map;
	
	private PropertieUtil() {
	}

	public static Map<String, Object> getPropertie(){
		return map;
	}
	
	@PostConstruct
	public void init(){
		readProperties("db.properties");
	}
	/**
	 * 读取配置文件某属性
	 */
	public static String readValue(String filePath, String key) {
		Properties props = new Properties();
		try {
			// 注意路径以 / 开始,没有则处理
			if (!filePath.startsWith("/"))
				filePath = "/" + filePath;
			InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
			props.load(in);
			String value = props.getProperty(key);
			return value;
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}

	/**
	 * 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties)
	 */
	public static void readProperties(String filePath) {
		map = new HashMap<>();
		Properties props = new Properties();
		try {
			// 注意路径以 / 开始,没有则处理
			if (!filePath.startsWith("/"))
				filePath = "/" + filePath;
			InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
			props.load(in);
			Enumeration<?> en = props.propertyNames();
			// 遍历打印
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String value = props.getProperty(key);
				map.put(key, value);
				logger.info(key + ":" + value);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * 将值写入配置文件
	 */
	public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
		// 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下
		// 注意路径不能加 / 了,加了则移除掉
		if (fileName.startsWith("/"))
			fileName.substring(1);
		String filePath = PropertieUtil.class.getResource("/").getPath() + fileName;
		// 获取配置文件
		Properties pps = new Properties();
		InputStream in = new BufferedInputStream(new FileInputStream(filePath));
		pps.load(in);
		in.close();
		OutputStream out = new FileOutputStream(filePath);
		// 设置配置名称和值
		pps.setProperty(parameterName, parameterValue);
		// comments 等于配置文件的注释
		pps.store(out, "Update " + parameterName + " name");
		out.flush();
		out.close();
	}

	public static void main(String[] args) throws Exception {
		readProperties("db.properties");
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		System.out.println(readValue("db.properties", "jdbc.driver"));
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		// writeProperties("conf/test.properties", "dataSource.driver", "test");
		//readProperties("conf/test.properties");
	}
}

需要用时:我在PersonController,方法调用

@RequestMapping("/queryByPage")
    @ResponseBody
    public ServerResponse<Page<Person>> queryByPage(@RequestBody PageParam<PersonParam> pageParam) {
    	Map<String, Object> map = PropertieUtil.getPropertie();
    	System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		System.out.println(map.get("jdbc.driver"));
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");
		System.out.println("---------------------------------");

        return personService.queryByPage(pageParam);
    }

测试输出,完美。。。。。。

猜你喜欢

转载自blog.csdn.net/qq_24241631/article/details/87929575
今日推荐