项目配置文件properties、yml配置文件获取Key参数的值工具类

本文介绍了如何创建一个Java工具类,用于从properties和yml配置文件中动态获取Key参数的值,提高代码可维护性和复用性。通过实例演示了如何读取`SystemFlg`配置,并展示了配置文件在实际项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目配置文件properties、yml配置文件获取Key参数的值工具类


前言

一、什么是properties配置文件?

介绍:在我们的项目当中,通常需要一些常量参数,然后在程序中使用常量参数,但是如何获取呢,下面介绍实现方式。

二、创建工具类

1.创建 IfbPropertiesUtil.java工具类

代码如下:读取zhx.properties配置文件

package com.ruimin.ifb.util;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import com.ruimin.ifs.core.log.SnowLog;

/**
 * 读取zhx.properties配置文件
 *
 */
public class IfbPropertiesUtil {
	private static final Logger logger = SnowLog.getLogger(IfbPropertiesUtil.class);
	private static final String configPath = "/zhx.properties";
	private static Properties prop = null;

	private static void init() throws Exception {
		InputStream in = null;
		try {
			in = IfbPropertiesUtil.class.getResourceAsStream(configPath);
			if (in != null) {
				prop = new Properties();
				prop.load(in);
				in.close();
				in = null;
			}
			if (prop == null) {
				throw new Exception("read [" + configPath + "] failed!");
			}
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

	public static String getString(String key) {
		try {
			if (prop == null) {
				init();
			}
			return prop.getProperty(key);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}
	
	public static Integer getInt(String key) {
		try {
			if (prop == null) {
				init();
			}
			return Integer.valueOf(prop.getProperty(key));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static boolean getBoolean(String key) {
		String val = getString(key);
		return "true".equalsIgnoreCase(val) || "1".equalsIgnoreCase(val) || "Y".equalsIgnoreCase(val);
	}

	public static boolean getBoolean(String key, boolean defValue) {
		String val = getString(key);
		if (val == null) {
			return defValue;
		} else {
			return getBoolean(key);
		}
	}

	public static String getString(String key, String defValue) {
		try {
			if (prop == null) {
				init();
			}
			if (prop.containsKey(key)) {
				return prop.getProperty(key);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return defValue;
	}
	
	public static Properties getProperty() {
		try {
			if (prop == null) {
				init();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return prop;
	}
}

2.测试实现效果

1、配置文件参数 SystemFlg
在这里插入图片描述

package test;

import com.ruimin.ifb.util.IfbPropertiesUtil;

/**
 * @author Mr.Zhx
 * @version 1.0
 * @description: 测试类
 * @create: 2022-03-21 17:07
 */
public class ZhxCeshi {
    public static void main(String[] args) {
        String systemFlg = IfbPropertiesUtil.getString("SystemFlg");
        System.out.println("properties配置文件 SystemFlg 的参数为:"+systemFlg);
    }
}

2、结果截图
在这里插入图片描述

总结

提示:可以复制代码,创建工具类 ,在项目当中引用即可实现功能,不要单独处理,使用工具类避免代码侵入性太高,复用性不高:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰太狼RD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值