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

项目配置文件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、结果截图
在这里插入图片描述

总结

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用SpringBoot引入Redis工具类时,首先需要在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- SpringBoot Data Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> ``` 接下来,在SpringBoot的配置类中添加Redis的配置信息,通常是application.properties或application.yml文件。示例: ```properties # Redis配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database=0 spring.redis.password= spring.redis.timeout=30000 ``` 然后,创建一个RedisUtil工具类,用来封装Redis的常用操作方法。可以使用Jedis库对Redis进行操作。示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisUtil { @Autowired private StringRedisTemplate stringRedisTemplate; // 存储键对 public void set(String key, String value) { stringRedisTemplate.opsForValue().set(key, value); } // 获取键对应的 public String get(String key) { return stringRedisTemplate.opsForValue().get(key); } // 删除键对 public void delete(String key) { stringRedisTemplate.delete(key); } // 判断键是否存在 public boolean exists(String key) { return stringRedisTemplate.hasKey(key); } } ``` 最后,可以在其他的Spring组件中使用RedisUtil来进行Redis操作。示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/example") public class ExampleController { @Autowired private RedisUtil redisUtil; @RequestMapping("/test") public String test() { // 存储键对 redisUtil.set("key", "value"); // 获取键对应的 String value = redisUtil.get("key"); return value; } } ``` 这样,就可以在SpringBoot中引入Redis工具类,并进行常用的Redis操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

灰太狼RD

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

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

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

打赏作者

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

抵扣说明:

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

余额充值