先上代码
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.util.Properties;
public class PropertiesUtil {
private static Properties props = new Properties();
static {
try {
ClassPathResource configResource = new ClassPathResource("/config.properties");
PropertiesLoaderUtils.fillProperties(props, configResource);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String key) {
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
return props.getProperty(key, defaultValue);
}
}
这只是实现了基础功能,从设计角度来说,可以把获取的值放到缓存里面,参考这篇博客
https://blog.csdn.net/baidu_30809315/article/details/76962874
对于有的同学说中文乱码,可以参考