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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils;
import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.*;
@Slf4j @Component public class ExternalPropertiesRefresh {
@Autowired private ConfigurableListableBeanFactory configurableListableBeanFactory;
private Object getFieldValueByName(String fieldName, Object object) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = "get" + firstLetter + fieldName.substring(1); Method method = object.getClass().getMethod(getter, new Class[]{}); Object value = method.invoke(object, new Object[]{}); return value; } catch (Exception e) { log.error(e.getMessage(), e); return null; } }
private void setFieldValueByName(String fieldName, Object object, Class[] paramTypes, Object[] params) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String setter = "set" + firstLetter + fieldName.substring(1); Method method = object.getClass().getMethod(setter, paramTypes); method.invoke(object, params);
} catch (Exception e) { log.error(e.getMessage(), e); } }
private String fieldName(String key, String prefix) { if (StringUtils.hasText(prefix)) { return key.replace(prefix + ".", ""); } return key; }
private Object bind(Object bean, Properties[] properties, String prefix) { String fieldName = ""; String pValue = ""; String[] sp = null; for (Properties pro : properties) { Map<String, Map<String, String>> fidleMap = new HashMap<>(); Map<String, Set<String>> fidleSet = new HashMap<>(); Map<String, List<String>> fidleList = new HashMap<>(); for (Object key : pro.keySet()) { pValue = (String) (pro.get(key)); fieldName = fieldName((String) key, prefix);
sp = fieldName.split("\\."); if (sp.length == 2) { fieldName = sp[0]; }
if (fieldName.indexOf("[") > 0) { fieldName = fieldName.substring(0, fieldName.indexOf("[")); }
Object object = getFieldValueByName(fieldName, bean);
if (object instanceof Map) { if (fidleMap.get(fieldName) != null) { object = fidleMap.get(fieldName); } else { object = new HashMap<String, String>(); } if (sp.length == 2) { ((Map) object).put(sp[1], pValue); fidleMap.put(fieldName, (Map<String, String>) object); } } else if (object instanceof Set) { if (fidleSet.get(fieldName) != null) { object = fidleSet.get(fieldName); } else { object = new HashSet<String>(); } ((Set) object).add(pValue); fidleSet.put(fieldName, (Set<String>) object); } else if (object instanceof List) { if (fidleList.get(fieldName) != null) { object = fidleList.get(fieldName); } else { object = new ArrayList<String>(); } ((List) object).add(pValue); fidleList.put(fieldName, (List<String>) object); } else if (object instanceof String) { setFieldValueByName(fieldName, bean, new Class[]{String.class}, new Object[]{pValue}); } else if (object instanceof Integer) { setFieldValueByName(fieldName, bean, new Class[]{Integer.class}, new Object[]{Integer.valueOf(pValue)}); } else if (object instanceof Long) { setFieldValueByName(fieldName, bean, new Class[]{Long.class}, new Object[]{Long.valueOf(pValue)}); } else if (object instanceof Double) { setFieldValueByName(fieldName, bean, new Class[]{Double.class}, new Object[]{Double.valueOf(pValue)}); } else if (object instanceof Float) { setFieldValueByName(fieldName, bean, new Class[]{Float.class}, new Object[]{Float.valueOf(pValue)}); } }
if (fidleMap.size() > 0) { for (String fname : fidleMap.keySet()) { setFieldValueByName(fname, bean, new Class[]{Map.class}, new Object[]{fidleMap.get(fname)}); } }
if (fidleSet.size() > 0) { for (String fname : fidleSet.keySet()) { setFieldValueByName(fname, bean, new Class[]{Set.class}, new Object[]{fidleSet.get(fname)}); } }
if (fidleList.size() > 0) { for (String fname : fidleList.keySet()) { setFieldValueByName(fname, bean, new Class[]{List.class}, new Object[]{fidleList.get(fname)}); } }
}
return bean; }
@SneakyThrows public void refresh(String beanName){ Class<?> cls = configurableListableBeanFactory.getType(beanName); Object bean = configurableListableBeanFactory.getBean(cls); Properties[] propertiesArray = null; String prefix = ""; if (cls.getAnnotations() != null && cls.getAnnotations().length > 0) { for (Annotation annotation : cls.getAnnotations()) {
if (annotation instanceof PropertySource) { PropertySource propertySource = (PropertySource) annotation; String[] values = propertySource.value(); if (values.length > 0) { propertiesArray = new Properties[values.length]; for (int i = 0; i < values.length; i++) { if (values[i].startsWith("file:")) { String path = values[i].replace("file:", ""); Properties properties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(path)); propertiesArray[i] = properties; } } } }
if (annotation instanceof ConfigurationProperties) { ConfigurationProperties configurationProperties = (ConfigurationProperties) annotation; prefix = configurationProperties.prefix(); }
} }
if (propertiesArray != null && propertiesArray.length > 0) { bind(bean, propertiesArray, prefix);
} }
@SneakyThrows public void refresh() { String[] ary = configurableListableBeanFactory.getBeanNamesForAnnotation(PropertySource.class); if (ary != null && ary.length > 0) { for (String beanName : ary) { refresh(beanName); } } } }
|