系列文章地址:https://blog.csdn.net/perfect2011/article/details/124603278
在日常开发中经常有这样那样的小功能需要实现,这些一般会作为工具类存在,在项目中有一些通用的功能,Spring内置了需要工具类,而且经过了大量的验证,可以在开发中助你一臂之力,代码也比较稳定,学习下这些工具类,让你的工作如虎添翼
这是一篇归纳的文章,直接上,因为工具类太多,也不好记忆,所以分几个模块,可以参考目录关注自己想看的工具类。
大部分的工具类在Spring-core包下,可以在IDE中打开这些类,更详细的了解,这边介绍一些常用的工具方法
1、签名解码
1.1 DigestUtils 加密算法工具类
byte[] shas = MessageDigest.getInstance("MD5").digest("香菜".getBytes());
String encodekey = DigestUtils.md5DigestAsHex("md5".getBytes());System.out.println(encodekey);
常见的对称加密算法:DES、3DES、DESX、Blowfish、IDEA、RC4、RC5、RC6和AES
常见的非对称加密算法:RSA、ECC(移动设备用)、Diffie-Hellman、El Gamal、DSA(数字签名用)
常见的Hash算法:MD2、MD4、MD5、HAVAL、SHA、SHA-1、HMAC、HMAC-MD5、HMAC-SHA1
1.2 Base64Utils
用于Base64解码和编码,直接看提供了哪些方法,解码使用decode,编码使用encode

2、文件操作
2.1 FileCopyUtils FileSystemUtils
FileCopyUtils 提供了多个将文件内容拷贝到各种目标对象中的方法

FileSystemUtils 主要文件夹的操作,拷贝和删除

2.2 StreamUtils 流的读取,写入
StreamUtils是spring中用于处理流的类

2.3 SerializationUtils 序列化
deserialize 反序列化serialize 序列化
3、数据转换
3.1 NumberUtils 数字转换之类
这个类看起来很明白了,方法名也很直白,基本是字符串转换

3.2 ObjectUtils 判断类型
主要是对象的判空和比较
3.3 StringUtils 字符串
这个是最常用的字符串类了,内容很多,就不一一列举了
3.4 PatternMatchUtils 正则表达式工具类

3.5 CollectionUtils集合工具类
这个集合类看名字吧,都是一些集合方面的操作,map,list,Array
4、反射工具类
4.1 ReflectionUtils 反射手术刀
这个类主要用来做一些反射工作,比如方法的查找,调用方法,访问属性等
下面调几个典型的,这里面有很多的重载方法,根据自己的需求选用
// 在类中查找指定方法
Method findMethod(Class<?> clazz, String name)
// 提供方法参数
Object invokeMethod(Method method, Object target, Object... args)
//查找字段
Field findField(Class<?> clazz, String name, Class<?> type)
// 获取 target 对象的 field 属性值
Object getField(Field field, Object target)
4.2 ClassUtils
这个主要用来获取class的属性
// 获取对象的所有接口
Class<?>[] allInterfaces = ClassUtils.getAllInterfaces(new Player());
// 获取某个类的包名
String packageName = ClassUtils.getPackageName(Player.class);
// 判断某个类是否内部类
ClassUtils.isInnerClass(Player.class);
// 判断对象是否代理对象
ClassUtils.isCglibProxy(new Player());
4.3 TypeUtils
主要是类型继承的关系类,平常用的不多

4.4 AnnotatedElementUtils和 AnnotationUtils
看名字也知道是处理注解相关的,在开发一些框架性的功能时,自定义一个注解做一些功能,使用这个工具类简直不要太轻松
这两个类的函数非常丰富,很多重载函数
4.5 BeanUtils
主要是bean的一些操作,这个是Spring-bean包下的

5、其他
5.1 ResourceUtils 资源访问
主要是资源访问的方法,它支持“classpath:”和“file:”的地址前缀,它能够从指定的地址加载文件资源,主要有两种,一种是获取文件,一种是判断

5.2 SystemPropertyUtils占位符解析工具类
这个工具类看名字是解析系统属性的,主要原理是通过依赖PropertyPlaceholderHelper
SystemPropertyUtils.resolvePlaceholders("${os.name}/test")
6、总结
这些工具类的方法贼多,记不住,不过有所谓,只要在需要使用的时候去对应的工具类查找就行了
不用要求自己都记住
赠人玫瑰,手留余香,求关注,点赞,收藏