关键知识点提炼:
- SpringBoot提供的工具类:ObjectUtils、StringUtils、CollectionUtils、ReflectionUtils、BeanUtils、MapUtils
- 自定义工具类:BeanUtil(包装BeanUtils)、PainationUtil(分页查询处理)、FeatureUtil(扩展字段处理)
- 断言
-
对象、数组、集合
-
ObjectUtils:获取对象基本信息
-
StringUtils:字符串处理
---------字符串判断工具--------------------------- // 判断字符串是否为 null,或 ""。注意,包含空白符的字符串为非空 boolean isEmpty(Object str) // 判断字符串是否是以指定内容结束。忽略大小写 boolean endsWithIgnoreCase(String str, String suffix) // 判断字符串是否已指定内容开头。忽略大小写 boolean startsWithIgnoreCase(String str, String prefix) // 是否包含空白符 boolean containsWhitespace(String str) // 判断字符串非空且长度不为 0,即,Not Empty boolean hasLength(CharSequence str) // 判断字符串是否包含实际内容,即非仅包含空白符,也就是 Not Blank boolean hasText(CharSequence str) // 判断字符串指定索引处是否包含一个子串。 boolean substringMatch(CharSequence str, int index, CharSequence substring) // 计算一个字符串中指定子串的出现次数 int countOccurrencesOf(String str, String sub) ---------字符串操作工具--------------------------- // 查找并替换指定子串 String replace(String inString, String oldPattern, String newPattern) // 去除尾部的特定字符 String trimTrailingCharacter(String str, char trailingCharacter) // 去除头部的特定字符 String trimLeadingCharacter(String str, char leadingCharacter) // 去除头部的空白符 String trimLeadingWhitespace(String str) // 去除头部的空白符 String trimTrailingWhitespace(String str) // 去除头部和尾部的空白符 String trimWhitespace(String str) // 删除开头、结尾和中间的空白符 String trimAllWhitespace(String str) // 删除指定子串 String delete(String inString, String pattern) // 删除指定字符(可以是多个) String deleteAny(String inString, String charsToDelete) // 对数组的每一项执行 trim() 方法 String[] trimArrayElements(String[] array) // 将 URL 字符串进行解码 String uriDecode(String source, Charset charset) ---------路径相关工具方法--------------------------- // 解析路径字符串,优化其中的 “..” String cleanPath(String path) // 解析路径字符串,解析出文件名部分 String getFilename(String path) // 解析路径字符串,解析出文件后缀名 String getFilenameExtension(String path) // 比较两个两个字符串,判断是否是同一个路径。会自动处理路径中的 “..” boolean pathEquals(String path1, String path2) // 删除文件路径名中的后缀部分 String stripFilenameExtension(String path) // 以 “. 作为分隔符,获取其最后一部分 String unqualify(String qualifiedName) // 以指定字符作为分隔符,获取其最后一部分 String unqualify(String qualifiedName, char separator)
-
-
CollectionUtils
-
文件、资源、IO 流
-
FileCopyUtils
-
ResourceUtils
-
StreamUtils
-
-
反射、AOP
-
ReflectionUtils
---------------获取方法-------------------------------------- // 在类中查找指定方法 Method findMethod(Class<?> clazz, String name) // 同上,额外提供方法参数类型作查找条件 Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) // 获得类中所有方法,包括继承而来的 Method[] getAllDeclaredMethods(Class<?> leafClass) // 在类中查找指定构造方法 Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) // 是否是 equals() 方法 boolean isEqualsMethod(Method method) // 是否是 hashCode() 方法 boolean isHashCodeMethod(Method method) // 是否是 toString() 方法 boolean isToStringMethod(Method method) // 是否是从 Object 类继承而来的方法 boolean isObjectMethod(Method method) // 检查一个方法是否声明抛出指定异常 boolean declaresException(Method method, Class<?> exceptionType) ---------------执行方法-------------------------------------- // 执行方法 Object invokeMethod(Method method, Object target) // 同上,提供方法参数 Object invokeMethod(Method method, Object target, Object... args) // 取消 Java 权限检查。以便后续执行该私有方法 void makeAccessible(Method method) // 取消 Java 权限检查。以便后续执行私有构造方法 void makeAccessible(Constructor<?> ctor) ---------------获取字段-------------------------------------- // 在类中查找指定属性 Field findField(Class<?> clazz, String name) // 同上,多提供了属性的类型 Field findField(Class<?> clazz, String name, Class<?> type) // 是否为一个 "public static final" 属性 boolean isPublicStaticFinal(Field field) ---------------设置字段-------------------------------------- // 获取 target 对象的 field 属性值 Object getField(Field field, Object target) // 设置 target 对象的 field 属性值,值为 value void setField(Field field, Object target, Object value) // 同类对象属性对等赋值 void shallowCopyFieldState(Object src, Object dest) // 取消 Java 的权限控制检查。以便后续读写该私有属性 void makeAccessible(Field field) // 对类的每个属性执行 callback void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) // 同上,多了个属性过滤功能。 void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff) // 同上,但不包括继承而来的属性 void doWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc)
-
AopUtils
-----------判断代理类型--------------------------------- // 判断是不是 Spring 代理对象 boolean isAopProxy() // 判断是不是 jdk 动态代理对象 isJdkDynamicProxy() // 判断是不是 CGLIB 代理对象 boolean isCglibProxy() -----------获取被代理对象的 class-------------------------------------------- // 获取被代理的目标 class Class<?> getTargetClass()
-
AopContext
获取当前对象的代理对象 Object currentProxy()
自定义工具类:
-
BeanUtil(包装BeanUtils)
-
package com.test.utils; import java.util.* import org.springframword.beans.Beanutils; import org.springframword.util.CollectionUtils; public class BeanUtil extends BeanUtils( public BeanUtil(){ } public statci<T, E> List<T> Dto2List(Class<T> destType, List<E> orig){ List<T> dest = new ArratList(); if (CollectionUtils.isEmpty(orig)){ return dest; }else { Iterator it = orig.iterator(); while(it.hasNext()){ E e = it.next(); T obj = BeanUtils.instantiateClase(destType); BeanUtils.copyProperties(e, obj); dest.add(obj); } return dest; } } public statci<T> T copyProperties(Class<T> destType, Object srcObject){ if (srcObject == null){ return null; } else { T obj = BeanUtils.instantiateClase(destType); BeanUtils.copyProperties(srcObject, obj); return obj; } }
-
PainationUtil(分页查询处理)、FeatureUtil(扩展字段处理)
-
有个注意点,慎用BeanUtils.copyProperties: