公众号赵侠客
弁言
Hutool这个工具应该是家喻户晓了,它是一个开源的Java工具类库,提供了丰富的静态工具方法来简化Java开发中的常见代码。本日原来是想尝鲜体验一下Hutool新版本都更新了什么新功能,于是从5.x.x升到到了6.0.0,然后就出现了各种包名不存在的错误,我其时就蒙了。
图:Hutool升级6.0后报错
追念当年Mybatis-Plus从2.0升级到3.0时所有包名及方法都需要改一遍的痛苦经历仍然念兹在兹,至今还有许多项目在使用Mybatis-Plus2.0,因为升级修改的太多了,真的太痛苦了,干脆就不升级了。难道Hutool要重蹈Mybatis-plus的覆辙了?如果你使用了Hutool,以后想要升级到6.0时,那么你将要把所有的包名都改一遍,因为Hutool从5.0升级到6.0后包名全改了!!!
图:StrUtil在Hutool 5.0包名为cn.hutool.*
Hutool的作者Looly在gitee上也回应了6.0包名修改,说是为了兼容性考虑!!!
图:Hutool 作者回应更改包名
为什么改包名?
作者说更改包名是为了大改后能兼容,那为什么包名改成org.dromara?。在搞清为什么要将包名改为org.dromara前我们需要搞清晰这个dromara是什么?从官网先容来看dromara是一个聚集了一群开源爱好者的非赢利性构造,可以以为是中国版的Apache,那么就很好理解了,开源项目参加Apache那包名肯定要改成org.apache了。
图:Dromara官网
从Dromara 发布的消息来看,Hutool早在2021年3月15号就参加了该构造
图:Dromara宣布Hutool参加
Dromara社区旗下的顶级项目除了Hutool外还有许多我们耳熟能详的项目,我用过的就有:流程引擎框架LiteFlow、ElasticSearch客户端easy-es。
图:Dromara社区的顶级项目
现在各人应该明白Hutool为什么要把包名改成org.dromara了吧?
重要改了哪些
- 包名修改,包名变更为org.dromara.hutool
- 重新整理规整,内部工具类重新整理规整,淘汰无用模块,工具类归类到对应package中
- 重构Http模块,被人诟病的http模块做重构,采用门面模式,支持包括HttpUrlConnection、HttpClient4、HttpClient5、OkHttp3等库。
- 性能优化,在本事范围之内尽量做性能优化,不跟其他高性能库“攀比”。
- 做减法,相比5.x版本做减法,大部门工作是删掉一些重复代码和无用的重载,使用上大概会增长代码量,但是相比淘汰了歧义
- 统一构造方法,构建一种对象不再使用杂乱的createXXX、newXXX、ofXXX等名字,而是统一使用of大概ofXXX。
包名修改、重新整理规整
Util工具类修改了包名,不但将所有的cn.hutool改成org.dromara.hutool,还按工具类的用途做了分包, 如字符串类工具类移到了org.dromara.hutool.core.text.,聚集类的移到了 org.dromara.hutool.core.collection,时间的移到org.dromara.hutool.core.date*
5.0常用工具类
-
- cn.hutool.core.util.StrUtil.isEmpty("admin");
- cn.hutool.core.collection.CollUtil.isEmpty(new ArrayList<>());
- cn.hutool.core.date.DateUtil.now();
复制代码 6.0常用工具类
- org.dromara.hutool.core.text.StrUtil.isEmpty("admin");
- org.dromara.hutool.core.collection.CollUtil.isEmpty(new ArrayList<>());
- org.dromara.hutool.core.date.DateUtil.now();
复制代码 重构Http模块
5.0HttpUtill使用方法
- cn.hutool.core.util.StrUtil.isEmpty("admin");
- cn.hutool.core.collection.CollUtil.isEmpty(new ArrayList<>());
- cn.hutool.core.date.DateUtil.now();
复制代码 6.0使用了门面模式+依赖SPI机制提供可选引擎
- //默认使用 org.dromara.hutool.http.client.engine.httpclient5.HttpClient5Engine
- org.dromara.hutool.http.HttpUtil.get("https://www.baidu.com");
-
- final ClientEngine engine = new OkHttpEngine();
- final Request req = Request.of("https://www.baidu.com/").method(Method.GET);
- final Response res = engine.send(req);
- log.info(res.body().getString());
- final ClientEngine jdkHttpEngine = HttpUtil.createClient("jdkClient");
- final Response send = jdkHttpEngine.send(Request.of("https://www.baidu.com").method(Method.GET));
- log.info(send.body().getString());
- //目前支持4种HTTP引擎
- org.dromara.hutool.http.client.engine.httpclient5.HttpClient5Engine
- org.dromara.hutool.http.client.engine.httpclient4.HttpClient4Engine
- org.dromara.hutool.http.client.engine.okhttp.OkHttpEngine
- org.dromara.hutool.http.client.engine.jdk.JdkClientEngine
复制代码 更多别的不兼容修改
StrUtil
- - StrUtil.utf8Bytes(data));
- + ByteUtil.toUtf8Bytes(data));
复制代码 NumberUtil
- - import cn.hutool.core.util.NumberUtil;
- + import org.dromara.hutool.core.math.NumberUtil;
复制代码 ArrayUtil
- - import import cn.hutool.core.util.ArrayUtil;
- + import import org.dromara.hutool.core.array.ArrayUtil;
复制代码 ObjectUtil
- - cn.hutool.core.util.ObjectUtil.isNotEmpty(obj)
- + org.dromara.hutool.core.util.ObjUtil.isNotEmpty(obj)
复制代码 lambda整体结构、命名都规范了,以SerXxx开头
- - import cn.hutool.core.lang.func.Func1;
- + import org.dromara.hutool.core.func.SerFunction;
- - import cn.hutool.core.lang.Filter;
- + import java.util.function.Predicate;
复制代码 修改Base64
- - cn.hutool.core.codec.Base64Encoder.encode(encryptAesKey);
- + org.dromara.hutool.core.codec.binary.Base64.encode(encryptAesKey);
- - cn.hutool.core.codec.Base64Decoder.decode(pubKey);
- + org.dromara.hutool.core.codec.binary.Base64.decode(pubKey);
复制代码 generateKey
- - cn.hutool.crypto.SecureUtil.generateKey(str)
- + org.dromara.hutool.crypto.KeyUtil.generateKey(str)
复制代码 反射利用field
- - cn.hutool.core.util.ReflectUtil.getFields(clazz)
- + org.dromara.hutool.core.reflect.FieldUtil.getFields(clazz)
- - ReflectUtil.setFieldValue(clientPrincipal, "authorities", authorities);
- + FieldUtil.setFieldValue(clientPrincipal, "authorities", authorities);
复制代码 日期时间
- - cn.hutool.core.date.LocalDateTimeUtil
- - cn.hutool.core.date.DateUtil.date()
- - cn.hutool.core.date.DateUtil;
- + org.dromara.hutool.core.date.DateUtil;
- + org.dromara.hutool.core.date.DateUtil.now();
复制代码 dfa
- - import cn.hutool.dfa.WordTree;
- + import org.dromara.hutool.core.text.dfa.WordTree;
- - cn.hutool.core.text.replacer.ReplacerChain#replace(str)
- - org.dromara.hutool.core.text.replacer.ReplacerChain#apply(str)
复制代码 Dict
- - import cn.hutool.core.lang.Dict;
- + import org.dromara.hutool.core.map.Dict;
复制代码 JSONObject
- - jsonObject.putOpt(key,value).putOpt(key,value)
复制代码 UtilException
- - cn.hutool.core.exceptions.UtilException
复制代码 FileUtil
- - import cn.hutool.core.io.FileUtil;
- + import org.dromara.hutool.core.io.file.FileUtil;
- - FileUtil.exist(file))
- + FileUtil.exists(file))
- -FileUtil.cleanEmpty
- - cn.hutool.core.io.FileUtil.extName(filePath)
- + org.dromara.hutool.core.io.file.FileNameUtil.extName(filePath)
复制代码 CollUtil
- - cn.hutool.core.collection.CollUtil.newArrayList(obj);
- + org.dromara.hutool.core.collection.ListUtil.of(obj);
- - CollUtil.split(phones, 200);
- + CollUtil.partition(phones, 200);
- -ArrayUtil.toArray
复制代码 tree
- - cn.hutool.core.lang.tree.Tree;
- - cn.hutool.core.lang.tree.TreeNode;
- - cn.hutool.core.lang.tree.TreeUtil;
- + org.dromara.hutool.core.tree.MapTree;
- + org.dromara.hutool.core.tree.TreeNode;
- + org.dromara.hutool.core.tree.TreeUtil;
复制代码 create -> of
- - cn.hutool.json.JSONConfig.create()
- - Xxx.create()
- + org.dromara.hutool.json.JSONConfig.of()
- + Xxx.of()
复制代码 ReUtil
- - cn.hutool.core.util.ReUtil.RE_CHINESE;
- + org.dromara.hutool.core.regex.PatternPool.CHINESE;
复制代码 DefaultSegment
- - cn.hutool.core.lang.DefaultSegment#getStartIndex
- + org.dromara.hutool.core.lang.range.DefaultSegment#getBeginIndex
复制代码 http
- - cn.hutool.http.HttpUtil.post(url, data, timeout)
- + org.dromara.hutool.http.HttpGlobalConfig.setTimeout(TIMEOUT);
- + org.dromara.hutool.http.HttpUtil.post(url, data)
- - cn.hutool.http.HttpUtil.toParams(map)
- + org.dromara.hutool.core.net.url.UrlQueryUtil.toQuery(map)
复制代码 BeanUtil
- - cn.hutool.core.bean.BeanUtil;
- - cn.hutool.core.bean.copier.CopyOptions;
- + org.dromara.hutool.core.bean.BeanUtil;
- + org.dromara.hutool.core.bean.copier.CopyOptions;
复制代码 EnumUtil
- - cn.hutool.core.util.EnumUtil;
- + org.dromara.hutool.core.util.EnumUtil;
复制代码 IdUtil
- - cn.hutool.core.util.IdUtil;
- + org.dromara.hutool.core.data.id.IdUtil;
复制代码 RegexPool
- - cn.hutool.core.lang.RegexPool
- + org.dromara.hutool.core.regex.RegexPool
复制代码 URLDecoder
- - cn.hutool.core.net.URLDecoder;
- + org.dromara.hutool.core.net.url.URLDecoder;
复制代码 SpringUtil
- - cn.hutool.extra.spring.SpringUtil;
- + org.dromara.hutool.extra.spring.SpringUtil;
复制代码 SecureUtil
- - cn.hutool.crypto.SecureUtil;
- - cn.hutool.crypto.asymmetric.KeyType;
- - cn.hutool.crypto.symmetric.AES;
- - cn.hutool.core.util.HexUtil;
- - cn.hutool.crypto.BCUtil;
- - cn.hutool.crypto.SmUtil;
- - cn.hutool.crypto.asymmetric.SM2;
- - cn.hutool.crypto.symmetric.SM4;
- + org.dromara.hutool.crypto.SecureUtil;
- + org.dromara.hutool.crypto.asymmetric.KeyType;
- + org.dromara.hutool.crypto.symmetric.AES;
- + org.dromara.hutool.core.codec.HexUtil;
- + org.dromara.hutool.crypto.asymmetric.SM2;
- + org.dromara.hutool.crypto.symmetric.SM4;
- + org.dromara.hutool.crypto.bc.BCUtil;
- + org.dromara.hutool.crypto.bc.SmUtil;
复制代码 ImgUtil
- - cn.hutool.core.img.ImgUtil;
- + org.dromara.hutool.swing.img.ImgUtil;
复制代码 RandomUtil
- - cn.hutool.core.util.RandomUtil;
- + org.dromara.hutool.core.util.RandomUtil;
- - cn.hutool.core.io.IORuntimeException;
- - cn.hutool.system.SystemUtil;
- - RandomUtil.randomEleList(WORDS, wordCount);
- + RandomUtil.randomEles(WORDS, wordCount);
复制代码 Captcha
- - cn.hutool.core.img.FontUtil;
- - cn.hutool.captcha.CaptchaUtil;
- - cn.hutool.captcha.CircleCaptcha;
- - cn.hutool.captcha.GifCaptcha;
- - cn.hutool.captcha.LineCaptcha;
- - cn.hutool.captcha.ShearCaptcha
- + org.dromara.hutool.swing.captcha.CaptchaUtil;
- + org.dromara.hutool.swing.captcha.CircleCaptcha;
- + org.dromara.hutool.swing.captcha.GifCaptcha;
- + org.dromara.hutool.swing.captcha.LineCaptcha;
- + org.dromara.hutool.swing.captcha.ShearCaptcha
复制代码 IoUtil
- - cn.hutool.core.io.IoUtil;
- + org.dromara.hutool.core.io.IoUtil;
复制代码 sm2
- - (sm2.decryptFromBcd(content, KeyType.PrivateKey));
- + (sm2.decrypt(content, KeyType.PrivateKey));
- - sm2.encryptBcd(content, KeyType.PublicKey);
- + sm2.encryptHex(content, KeyType.PublicKey);
复制代码 最后
国产开源软件能靠自己活下来都挺不容易,被开源构造收编也是不错的出路,就是苦了我们这些开发者,全国使用Hutool的项目不在少数,针对这次重大改动本子提出一下几点发起:
- 重度依赖,这种项目来说最好就不要改了,继续使用5.0,作者说了5.0也会继续维护
- 轻度依赖,如果只是用了一些简单的工具类,全局替换包名升级照旧可以的
- 新项目,我们需要思索有没有须要使用Hutool,是不是可以使用JDK17?许多新功能JDK都自带了,自己项目组是不是也应该维护起属于自己团队的工具类?
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |