问题: 动态修改配置文件,让服务感知,动态刷新内存中的数据,立马生效
此时需要手动将properties 转 bean , 替换内存数据
eg:
properties:
name = sea addr= sz a.mylist[0].a = l1 a.mylist[0].b = l2
...
json:
{ "mylist": [ { "b": "l2", "a": "l1" }, { "b": "l22", "a": "l11" } ], "name": "sea", "addr": "sz" }
此处采用第三方依赖:(建议自己实现)
依赖:
<dependency> <groupId>pl.jalokim.propertiestojson</groupId> <artifactId>java-properties-to-json</artifactId> <version>5.2.1</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency>
使用:
import pl.jalokim.propertiestojson.util.PropertiesToJsonConverter; ... // properties as Map<String,String>, java.util.Properties, java.io.InputStream String json = new PropertiesToJsonConverter().convertToJson(properties); // convert from file String jsonFromProperties = new PropertiesToJsonConverter().convertPropertiesFromFileToJson("/home/user/file.properties"); String jsonFromProperties2 = new PropertiesToJsonConverter().convertPropertiesFromFileToJson(new File("/home/user/file.properties")); // for map with Object as value, String as key Map<String,Object> valuesAsObjectMap = new HashMap<>(); String jsonFromProperties3 = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(valuesAsObjectMap); // converter Instance can be gathered through PropertiesToJsonConverterBuilder class, it has a few method for customization PropertiesToJsonConverter propsToJsonConverter = PropertiesToJsonConverterBuilder.builder().build();
PropsUtils:
@ToString @Data static class Sea { private String name; private String addr; private List mylist; } @Test public void testName() throws Exception { Properties properties = new Properties(); properties.put("user1.name", "sea"); properties.put("user.name", "sea"); properties.put("user.addr", "sz"); properties.put("user.mylist[0].a", "l1"); properties.put("user.mylist[0].b", "l2"); properties.put("user.mylist[1].a", "l11"); properties.put("user.mylist[1].b", "l22"); Enumeration<Object> keys = properties.keys(); String prefix = "user"; Sea propsToBean = propsToBean(properties, prefix, Sea.class); System.err.println(propsToBean); } public static String propsToJson(Properties properties,String prefix) throws Exception { Enumeration<Object> keys = properties.keys(); Properties newProps = new Properties(); while ( keys.hasMoreElements()) { String key = (String) keys.nextElement(); if(key.startsWith(prefix+".")) { newProps.put(key.substring(prefix.length()+1),properties.get(key)); } } String json = new PropertiesToJsonConverter().convertToJson(newProps); return json; } public static <T> T propsToBean(Properties properties,String prefix,Class<T> clazz) throws Exception { Enumeration<Object> keys = properties.keys(); Properties newProps = new Properties(); while ( keys.hasMoreElements()) { String key = (String) keys.nextElement(); if(key.startsWith(prefix+".")) { newProps.put(key.substring(prefix.length()+1),properties.get(key)); } } String json = new PropertiesToJsonConverter().convertToJson(newProps); T bean = JSON.parseObject(json,clazz); return bean; }