Java获取项目properties配置文件属性、获取外部配置文件,无需重启系统实现刷新配置文件

目标:springboot打包发布后,可以读取jar外部的配置文件中的属性,并实现不重启更新配置文件。

项目结构:

项目结构

实现方法:

package com.bdxh.fileserver.utils;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 功能说明:资源工具类
 */
public final class ConfigMgr {

	private static Logger logger = LoggerFactory.getLogger(ConfigMgr.class);
	private static Properties properties = new Properties();

	public static final String LOCATIONS = "file:./config/,file:./,classpath:/config/,classpath:/";

	private static String DEFAULT_MESSAGE = null;

	static {
		try {
			List<String> locs = IdUtil.splitStrings(LOCATIONS);
			for (String location : locs) {
				properties = loadProperties(location);
				if (properties != null) {
					break;
				}
			}
		} catch (Exception e1) {
			logger.error("配置文件加载出错", e1);
		}
	}

	public static Properties loadProperties(String location) {
		try {
			String s = "Load resource file - " + location;
			logger.info(s);
			return PropertiesLoaderUtils.loadProperties(new PathMatchingResourcePatternResolver().getResource(location + "application.properties"));
		} catch (Exception e) {
			String s = "Load resource file failed - " + location;
			logger.error(s);
			return null;
		}
	}

	public static void reloadProperties() {
		Properties reloadProperties = new Properties();
		List<String> locs = IdUtil.splitStrings(LOCATIONS);
		for (String location : locs) {
			try {
				reloadProperties = PropertiesLoaderUtils.loadProperties(new PathMatchingResourcePatternResolver().getResource(location + "application.properties"));

			} catch (Exception e) {
				String s = "Load resource file failed - ";
				logger.error(s);
			}
			if (reloadProperties != null && reloadProperties.size()>0) {
				properties = reloadProperties;
				String s = "Load resource file success - application.properties";
				logger.info(s);
				break;
			}
		}

	}

	public static String getResource(String key) {
		String message = properties.getProperty(key, DEFAULT_MESSAGE);
		try {
			message = new String(message.getBytes(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return DEFAULT_MESSAGE;
		}
		return message;
	}

	public static String getResource(String key, String defaultValue) {
		String message = properties.getProperty(key, defaultValue);
		try {
			message = new String(message.getBytes(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return defaultValue;
		}
		return message;
	}

}

在线刷新配置文件:

写一个controller调用下 上面工具类的reloadProperties()方法即可

@RequestMapping("/reloadConfig")
    @ResponseBody
    public String reloadConfig(){
        try {
            ConfigMgr.reloadProperties();
        } catch (Exception e1) {
            logger.error("配置文件加载出错", e1);
            return "ReloadConfig failed -"+e1;
        }
        return "ReloadConfig Success ";
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值