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

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

项目结构:

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

实现方法:

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

你可能感兴趣的