Various Util tool class god-level framework Hutool

Hutool is a homophonic "confused", which implies the pursuit of the state of "being confused in everything, no loss, no gain".

Hutool is a Java toolkit, but also just a toolkit. It helps us simplify every line of code, reduce every method, and make the Java language "sweet". Hutool was originally a compilation of the "util" package in my project, and then gradually accumulated and added more non-business related functions, and extensively learned the essence of other open source projects, and after self-organizing and modifying, finally formed a rich open source tool set. (FROM from the author's introduction)

Features

A Java basic tool class that encapsulates JDK methods such as file, stream, encryption and decryption, transcoding, regular, thread, XML, etc., to form various Util tool classes, and provides the following components:

  • hutool-aop JDK dynamic proxy package, providing non-IOC aspect support

  • hutool-bloomFilter Bloom filter, provides some Bloom filters of Hash algorithm

  • hutool-cache cache

  • The core of hutool-core, including Bean operations, dates, various Utils, etc.

  • hutool-cron timing task module, providing timing tasks like Crontab expressions

  • hutool-crypto encryption and decryption module

  • Data operation after hutool-db JDBC encapsulation, based on the idea of ​​ActiveRecord

  • hutool-dfa Multi-keyword search based on DFA model

  • hutool-extra extension module, package for third parties (template engine, email, etc.)

  • hutool-http Http client package based on HttpUrlConnection

  • hutool-log automatically recognizes the log facade implemented by the log

  • hutool-script script execution package, such as Javascript

  • hutool-setting More powerful Setting configuration file and Properties package

  • hutool-system system parameter call package (JVM information, etc.)

  • hutool-json JSON implementation

  • Implementation of hutool-captcha image verification code

Simple test

In the past two days, I have used Hutool to replace some of the codes in Halo. I have to say that it is very smooth to use. Here is a brief introduction to some of the Hutool tools I used.

SecureUtil (encryption and decryption tool)

It is mainly used when logging in and when changing the password. Because the password in the database is encrypted by md5, so when logging in, you need to encrypt it before querying the database. If you use Hutool, you only need to call SecureUtil. The md5 method will do.

user = userService.userLoginByName(loginName,SecureUtil.md5(loginPwd));

HtmlUtil (HTML tool class)

This tool class is more powerful, but the one I use most in Halo is HtmlUtil.encode, which can convert some characters into safe characters to prevent xss injection and SQL injection, such as the comment submission below.

comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));

This is to prevent a little bad guy from deliberately writing some executable js code, and then submit a comment, this code will be executed in the background panel, which is more dangerous. Use the encode method to convert the tag into, so that after the conversion, the js code is It won't be executed anymore.

In addition, HtmlUtil also provides the following methods, you can try it if you are interested.

  • HtmlUtil.restoreEscaped restores escaped HTML special characters

  • HtmlUtil.encode escapes HTML characters in text as safe characters

  • HtmlUtil.cleanHtmlTag clear all HTML tags

  • HtmlUtil.removeHtmlTag clears the specified HTML tag and the content surrounded by the tag

  • HtmlUtil.unwrapHtmlTag clears the specified HTML tag, excluding the content

  • HtmlUtil.removeHtmlAttr removes attributes in HTML tags

  • HtmlUtil.removeAllHtmlAttr remove all attributes of the specified tag

  • HtmlUtil.filter filters HTML text to prevent XSS attacks

CronUtil (timed task)

This tool is even more powerful. It does not need a framework like quartz to do timing tasks, and CronUtil does not require any other dependencies. You only need to create a configuration file under resources, and then turn on the timing task when the program starts. , Such as Halo's scheduled backup function (one backup at 1:00 a.m. every day).

cron.setting:

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 * * ?
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
 this.loadActiveTheme();
 this.loadOptions();
 this.loadFiles();
 this.loadThemes();
 //启动定时任务
 CronUtil.start();
 log.info("定时任务启动成功!");
}

For specific usage, please see the document http://hutool.mydoc.io/?t=255673

Okay, let’s introduce these three tools. If you are interested, you can try other tools. It's quite comprehensive. This should be the best tool library I have ever used. It is worth a try.

Official website address: http://www.hutool.cn/

Project actual combat

Recently, in refactoring the ruoyi-vue open source project, Hutool was used to replace various Util tool classes in the original project, and the small 5000 lines of code were directly deleted.

GitHub address: https://github.com/YunaiV/ruoyi-vue-pro

Guess you like

Origin blog.csdn.net/weixin_42073629/article/details/115223219