版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1、现在项目新建config文件,再在里面建立文件。
2、再在里面编写数据。
3、编写读取配置的方法。
public class PropertyUtils { public static String getPropertiesValue(String key, String fileName) { Properties p = new Properties(); File file = new File("config" + File.separator + (fileName == null ? "application" : fileName) + ".properties"); String filePath = file.getAbsolutePath(); try { InputStream is = new FileInputStream(filePath); p.load(is); } catch (Exception e) { e.printStackTrace(); } return p.getProperty(key); } public static String getPropertiesValue(String key){ return getPropertiesValue(key, null); } }
4、编写swagger测试。
@ApiOperation(value = "测试Key", notes = "测试Key") @ApiImplicitParams({ @ApiImplicitParam(name = "key", value = "key值", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "fileName", value = "文件名", dataType = "String", paramType = "query") }) @RequestMapping(value = "test", method = RequestMethod.GET) public AjaxJson testKey(@RequestParam(value = "key") String key, @RequestParam(value = "fileName", required = false) String fileName) { AjaxJson ajaxJson = new AjaxJson<>(); try { ajaxJson.setData(PropertyUtils.getPropertiesValue(key, fileName)); } catch (Exception e) { e.printStackTrace(); ajaxJson.error("失败: " + e.getMessage()); } return ajaxJson; }
5、测试结果。输入key值为test1。
6、在不重启程序的情况下,直接改变文件中test1的值。再进行测试,发现值立马变掉了。