工具类: package com.jjmc.dcl.util; import org.apache.log4j.Logger; import java.util.Locale; import java.util.ResourceBundle; /** * @description 动态读取properties文件 */ public class ResourceUtil { private static Logger logger= Logger.getLogger(ResourceUtil.class); private final static MyResourceBundleControl ctl = new MyResourceBundleControl(); private static ResourceBundle getBundle(String pro) { return ResourceBundle.getBundle(pro, Locale.getDefault(), ctl); } /** * 读取conf.properties * @param key * @return value */ public static String getConf(String key) { String string = ""; try { string = getBundle("messageConfig/message").getString(key); } catch (Exception e) { logger.error("getConf error", e); } return string; } /** * 重载控制器 */ private static class MyResourceBundleControl extends ResourceBundle.Control { /** * 如果在加载配置文件中时隔一秒钟文件内容将重新读取 */ @Override public long getTimeToLive(String baseName, Locale locale) { return 1000; } @Override public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) { return true; } } } 注意:string = getBundle("messageConfig/message").getString(key); 这句代码中,文件路径要正确。messageConfig/message是在resources下有一个文件夹messageConfig,里面有个文件message.properties 配置文件中的配置如下: #message message.username=admin message.password=admin message.url=www.xxxx.com 另外,引用配置文件中的变量方法为: String username = ResourceUtil.getConf("message.username"); String password = ResourceUtil.getConf("message.password"); String messageUrl = ResourceUtil.getConf("message.url");