/**
* 類中的某些字段不校驗 並返回空字段名
*/
public static List<String> validateProperty(Object validateObj, String... ignoreProperties) {
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(validateObj.getClass());
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
List<String> errList = new ArrayList<>();
for (PropertyDescriptor targetPd : targetPds) {
Method readMethod = targetPd.getReadMethod();
if (readMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(validateObj);
if (value instanceof String) {
if (StrUtil.isEmpty((String) value)) {
errList.add(validateObj.getClass().getSimpleName() + "中的" + targetPd.getName() + "不能為空");
continue;
}
}
if (value == null) {
errList.add(validateObj.getClass().getSimpleName() + "中的" + targetPd.getName() + "不能為空");
}
if (!targetPd.getName().equals("class"))
map.put(targetPd.getName(), value);
} catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
map.forEach((k, v) -> {
System.out.println(k + ":" + v);
});
return errList;
}
---------------------------------------------------------
偶得如上代碼 用以拿到一個類中的所有字段名和對應的值
直接看代碼 找到了
static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException {
CachedIntrospectionResults results = strongClassCache.get(beanClass);
if (results != null) {
return results;
}
results = softClassCache.get(beanClass);
if (results != null) {
return results;
}
results = new CachedIntrospectionResults(beanClass);
ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse;
if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) ||
isClassLoaderAccepted(beanClass.getClassLoader())) {
classCacheToUse = strongClassCache;
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe");
}
classCacheToUse = softClassCache;
}
// 在這一行之后 強引用Map添加了緩存
CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results);
return (existing != null ? existing : results);
}
-------------------
因此 當對象第一次來的時候 反射創建 后續走到之后 就會走緩存了