版权声明:本文为博主-阿飞云原创文章,未经博主不可转载,谢谢! https://blog.csdn.net/u010648555/article/details/78552699 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div id="content_views" class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p>动态加载properties文件内容,不需要重启服务!</p>
1 、Maven 工程,在resource下新建一个properties文件
target/classes/config.properties
user=dufy
phoneNo=123456789
- 1
- 2
- 3
- 4
2、新建解析properties文件的工具类
package com.dufy.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;
/**
*
*
* @author:dufyun
* @version:1.0.0
* @date 2017/11/16
* @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
*/
public class PropertiesUtil {
private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
private static final String CONFIG_NAME = "config.properties";
private static Properties prop;
private static Long lastModified = 0L;
/**
* 初始化加载配置文件
*/
private static void init() {
prop = new Properties();
String filepath = PropertiesUtil.class.getClassLoader().getResource(CONFIG_NAME).getPath();
log.info(filepath);
FileInputStream fis = null;
try {
fis = new FileInputStream(filepath);
prop.load(fis);
} catch (IOException e) {
log.error(CONFIG_NAME +"载入系统路径资源文件错误!");
e.printStackTrace();
}finally {
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 判断配置文件是否改动
* @return returnValue :true:改动过 ,false:没有改动过
*/
private static boolean isPropertiesModified() {
boolean returnValue = false;
File file = new File(PropertiesUtil.class.getClassLoader().getResource(CONFIG_NAME).getPath());
if (file.lastModified() > lastModified) {
log.info("修改CONFIG_NAME:{} 配置文件",CONFIG_NAME);
lastModified=file.lastModified();
returnValue = true;
}
return returnValue;
}
/**
* 根据key获取配置文件中的值
* @param key key值
* @return 返回value
*/
private static String getPropertyValue(String key) {
if (prop == null || isPropertiesModified()) {
init();
}
String value = prop.get(key).toString();
log.info("根据key获取value值, key:{} ,value:{}",key,value);
return value;
}
/**
* 获取配置文件中所有的value值
* @return 所有配置的value值
*/
private static String getPropertyAllValue() {
if (prop == null || isPropertiesModified()) {
init();
}
Collection<Object> values = prop.values();
log.info("CONFIG_NAME :{},配置的所有的values:{}",CONFIG_NAME,values.toString());
return values.toString();
}
/**
* 验证登录的手机号是否为测试手机号
* @param phoneNo 登录手机号
* @return true: 是测试账户, false:不是测试账户
*/
public static boolean validateLoginNo(String phoneNo){
boolean flag = false;
String allValue = getPropertyAllValue();
if(allValue.contains(phoneNo)){
flag = true;
}
return flag;
}
public static void main(String[] args) {
while (true){
try {
Thread.sleep(2000);
System.out.println("validateLoginNo : " + validateLoginNo("123456789"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
3、测试代码
public static void main(String[] args) {
while (true){
try {
Thread.sleep(2000);
System.out.println("validateLoginNo : " + validateLoginNo("123456789"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
启动服务,然后控制台打印
validateLoginNo : true;
然后在服务运行中,修改下面内容!
/target/classes/config.properties
phoneNo=987654321
validateLoginNo : true
16:22:43.652 [main] INFO c.s.m.l.c.c.util.PropertiesUtil - 修改CONFIG_NAME:config.properties 配置文件
16:22:43.653 [main] INFO c.s.m.l.c.c.util.PropertiesUtil - CONFIG_NAME :config.properties,配置的所有的values:[987654321]
validateLoginNo : false
- 1
- 2
- 3
- 4
- 5
如果您觉得这篇博文对你有帮助,请点个赞,谢谢!
如果帅气(美丽)、睿智(聪颖),和我一样简单善良的你看到本篇博文中存在问题,请指出,我虚心接受你让我成长的批评,谢谢阅读!
祝你今天开心愉快!
欢迎访问我的csdn博客,我们一同成长!
“不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!”