速读摘要文章源自JAVA秀-https://www.javaxiu.com/3531.html
点击上方"Java基基",选择"设为星标"做积极的人,而不是积极废人!Hutool是一个Java工具包,也只是一个工具包,它帮助我们简化每一行代码,减少每一个方法,让Java语言也可以"甜甜的"。就介绍这三个工具类,有兴趣的可以去试试其他的工具,这应该是我用过最好用的一个工具类库了,值得一试。最近在重构ruoyi-vue开源项目,就使用Hutool进行替代原项目中的各种Util工具类,直接删除了小5000行代码。文章源自JAVA秀-https://www.javaxiu.com/3531.html
原文约 4079 字 | 图片 7 张 | 建议阅读 9 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/3531.html
不要再封装各种 Util 工具类了,这个神级框架值得拥有!
点击关注 ? Java基基 文章源自JAVA秀-https://www.javaxiu.com/3531.html
点击上方“Java基基”,选择“设为星标”文章源自JAVA秀-https://www.javaxiu.com/3531.html
做积极的人,而不是积极废人!文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html 源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/3531.html
原创 | Java 2020 超神之路,很肝~文章源自JAVA秀-https://www.javaxiu.com/3531.html
中文详细注释的开源项目文章源自JAVA秀-https://www.javaxiu.com/3531.html
RPC 框架 Dubbo 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
网络应用框架 Netty 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
消息中间件 RocketMQ 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
数据库中间件 Sharding-JDBC 和 MyCAT 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
作业调度中间件 Elastic-Job 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
分布式事务中间件 TCC-Transaction 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
Eureka 和 Hystrix 源码解析文章源自JAVA秀-https://www.javaxiu.com/3531.html
Java 并发源码文章源自JAVA秀-https://www.javaxiu.com/3531.html
来源:网络文章源自JAVA秀-https://www.javaxiu.com/3531.html
功能文章源自JAVA秀-https://www.javaxiu.com/3531.html
简单测试文章源自JAVA秀-https://www.javaxiu.com/3531.html
SecureUtil(加密解密工具)文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil(HTML 工具类)文章源自JAVA秀-https://www.javaxiu.com/3531.html
CronUtil(定时任务)文章源自JAVA秀-https://www.javaxiu.com/3531.html
项目实战文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
Hutool 谐音 “糊涂”,寓意追求 “万事都作糊涂观,无所谓失,无所谓得” 的境界。文章源自JAVA秀-https://www.javaxiu.com/3531.html
Hutool 是一个 Java 工具包,也只是一个工具包,它帮助我们简化每一行代码,减少每一个方法,让 Java 语言也可以 “甜甜的”。Hutool 最初是我项目中 “util” 包的一个整理,后来慢慢积累并加入更多非业务相关功能,并广泛学习其它开源项目精髓,经过自己整理修改,最终形成丰富的开源工具集。(FROM 自作者简介)文章源自JAVA秀-https://www.javaxiu.com/3531.html
功能
一个 Java 基础工具类,对文件、流、加密解密、转码、正则、线程、XML 等 JDK 方法进行封装,组成各种 Util 工具类,同时提供以下组件:文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-aop JDK 动态代理封装,提供非 IOC 下的切面支持文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-bloomFilter 布隆过滤,提供一些 Hash 算法的布隆过滤文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-cache 缓存文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-core 核心,包括 Bean 操作、日期、各种 Util 等文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-cron 定时任务模块,提供类 Crontab 表达式的定时任务文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-crypto 加密解密模块文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-db JDBC 封装后的数据操作,基于 ActiveRecord 思想文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-dfa 基于 DFA 模型的多关键字查找文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-extra 扩展模块,对第三方封装(模板引擎、邮件等)文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-http 基于 HttpUrlConnection 的 Http 客户端封装文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-log 自动识别日志实现的日志门面文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-script 脚本执行封装,例如 Javascript文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-setting 功能更强大的 Setting 配置文件和 Properties 封装文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-system 系统参数调用封装(JVM 信息等)文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-json JSON 实现文章源自JAVA秀-https://www.javaxiu.com/3531.html
hutool-captcha 图片验证码实现文章源自JAVA秀-https://www.javaxiu.com/3531.html
简单测试
这两天使用 Hutool 把 Halo 里面的一些代码给替换掉了,不得不说,用起来十分顺心,下面简单介绍一下我用到的一些 Hutool 的工具类。文章源自JAVA秀-https://www.javaxiu.com/3531.html
SecureUtil(加密解密工具)
主要是在登录的时候还有修改密码的时候用到的,因为数据库里面的密码是 md5 加密处理的,所以登录的时候需要先加密之后再到数据库进行查询,使用 Hutool 的话,只需要调用 SecureUtil 中的 md5 方法就可以了。文章源自JAVA秀-https://www.javaxiu.com/3531.html
user = userService.userLoginByName(loginName,SecureUtil.md5(loginPwd));
HtmlUtil(HTML 工具类)
这个工具类就比较厉害了,不过我在 Halo 当中用得最多的还是 HtmlUtil.encode,可以将一些字符转化为安全字符,防止 xss 注入和 SQL 注入,比如下面的评论提交。文章源自JAVA秀-https://www.javaxiu.com/3531.html
comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));
这就是防止有小坏蛋故意写一些可执行的 js 代码,然后提交评论,在后台面板就会执行这一段代码,比较危险,使用 encode 方法就可以将 标签给转化成,这样转化之后,js 代码就不会执行了。文章源自JAVA秀-https://www.javaxiu.com/3531.html
另外,HtmlUtil 还提供了以下方法,有兴趣的可以去试一下。文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.restoreEscaped 还原被转义的 HTML 特殊字符文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.encode 转义文本中的 HTML 字符为安全的字符文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.cleanHtmlTag 清除所有 HTML 标签文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.removeHtmlTag 清除指定 HTML 标签和被标签包围的内容文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.unwrapHtmlTag 清除指定 HTML 标签,不包括内容文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.removeHtmlAttr 去除 HTML 标签中的属性文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.removeAllHtmlAttr 去除指定标签的所有属性文章源自JAVA秀-https://www.javaxiu.com/3531.html
HtmlUtil.filter 过滤 HTML 文本,防止 XSS 攻击文章源自JAVA秀-https://www.javaxiu.com/3531.html
CronUtil(定时任务)
这个工具就更厉害了,完全不需要类似 quartz 这样的框架来做定时任务,而且 CronUtil 也不需要任何其他依赖,只需要在 resources 下建一个配置文件,然后在程序启动的时候将定时任务开启就行了,如 Halo 的定时备份功能(每天凌晨 1 点备份一次)。文章源自JAVA秀-https://www.javaxiu.com/3531.html
cron.setting:文章源自JAVA秀-https://www.javaxiu.com/3531.html
cc.ryanc.halo.web.controller.admin.BackupController.backupResources = 0 0 1 * * ?cc.ryanc.halo.web.controller.admin.BackupController.backupDatabase = 0 0 1 * * ?cc.ryanc.halo.web.controller.admin.BackupController.backupPosts = 0 0 1 * * ?@Overridepublic void onApplicationEvent(ContextRefreshedEvent event){ this.loadActiveTheme(); this.loadOptions(); this.loadFiles(); this.loadThemes(); //启动定时任务 CronUtil.start(); log.info("定时任务启动成功!");}
具体使用方法请看文档 http://hutool.mydoc.io/?t=255673文章源自JAVA秀-https://www.javaxiu.com/3531.html
好了,就介绍这三个工具类,有兴趣的可以去试试其他的工具,挺全的,这应该是我用过最好用的一个工具类库了,值得一试。文章源自JAVA秀-https://www.javaxiu.com/3531.html
官网地址:http://www.hutool.cn/文章源自JAVA秀-https://www.javaxiu.com/3531.html
项目实战
最近在重构 ruoyi-vue 开源项目,就使用 Hutool 进行替代原项目中的各种 Util 工具类,直接删除了小 5000 行代码。文章源自JAVA秀-https://www.javaxiu.com/3531.html
GitHub 地址:https://github.com/YunaiV/ruoyi-vue-pro文章源自JAVA秀-https://www.javaxiu.com/3531.html
感兴趣的胖友可以关注一波,预计年前发布正式版本!文章源自JAVA秀-https://www.javaxiu.com/3531.html
= = 最近每天肝到 1 点多,男人至死都是少年!文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/3531.html
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/3531.html
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章源自JAVA秀-https://www.javaxiu.com/3531.html
文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/3531.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/3531.html

评论