Jakarta Project的Commons tools各个子项目简介

Apache Commons工具集提供了多种实用工具,旨在补充和完善Java标准库的功能。这些工具涵盖了从异常处理到数据库连接池等广泛领域,能够显著提高开发效率并减少代码量。本文将详细介绍其中几项关键工具的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apache 的 Jakarta Project  里面有很多子项目。其中的 Commons 着眼于提供各种很通用的函数/类,这

样大家可以在平时自己的项目中用。这些 tools 因为非常 common, 以至于无论你用哪种 framework ,你

可以用到其中的一部分,来节省自己的编码量,并且其代码质量也是非常不错的。

Commons tools 一部分出发点在于解决 JDK 中的各种不足:

1. Exception 不好用,不得不写很多 try catch, 而改成使用 C/C++ 常用的以返回值判断,可以少写很多代

码。这方面以 jdbc interface 最为臭名昭著。

2. 弥补 JDK 类库的不完善地方。比如 String , Collection Framework, 正则表达式等等。

打算写文章介绍其中几个我认为比较通用的东西:

Codec 包含 Hex, Base64, URL 的编码和反解码。对于 web 应用很有用处。

Commons-Collections 增强 jdk 的 Collection Framework 功能。

Commons-Configuration 读写配置参数。大多数程序都有运行参数放在某个地方,这个工具可以读写各种参数格式:数据库,xml ,Properties 文件,系统变量,等等。强吧?

Commons-DBCP 数据库连接池,呵呵,现在连接池很多服务器都自带。但也有很多直接用这个。如果写 java application 要用到数据库,用这个吧。

DbUtils 可以减少写 JDBC 程序的 try catch, 减少代码量。一个我想写一直没有时间写的东西。但没有想象中那么好。

Email 发电子邮件。JDK 标准库对于发电子邮件支持不强,很让人看不懂。

FileUpload 文件上传。J2EE 标准库对于文件上传支持不强,也让人看不懂。这两个功能对于现在的 j2ee 应用来说,是基本功能了。稍微大一点的系统都会有。

IO 虽然 jdk 号称 IO 重写了,并且还用了 nio(new IO) 的 package 名,但是还是不够好。这个可以看作是增强版了。

Commons-Lang 好东西。 lang 包是 java 最基本的包,默认自动 import , 功能还是不够强,补补!这个包在其他系统,包括 weblogic, websphere 中都大量用它。基本上凡是有点名气的 java 系统都会用它。

Commons-Logging 好东西。大多数 EJB server 和 tomcat 都用到它。开源的 java project 也大部分用它。log 的抽象层。Java 中的 log 以前有很多种,如果你选了一种开发程序,以后想换另一种,比较麻烦。通过这个工具抽象一把,就很容易换了。不过最近 log4j 一支独大,JDK 自带的 log 没有几个人用,似乎不会有人换其他的 log, 让人搞不懂。

Math 呵呵。Java 中的 Math 包只能勉强够用,如果稍微偏们一点的数学函数,都要自己写了。用它吧,节省时间。

Net 增强 Java 网络功能。Java 的网络类库比起专业一点的 C++ 网络类库,差别还是很大。这里还是增强一点为好。

Commons-Pool 普通的对象池。不是 EJB 的对象池。池这个东西,很难写好。弄不好特耗内存,速度反而慢(比如 EJB)。无论如何,是个好的思路,可以借鉴。

Validator 这是好东西。不管是写 web 应用程序还是普通 java 应用程序,只要有用户输入界面,都用得上。比自己写省事阿。不赞成写在 xml 中,多一个字母少一个字母很难查。

Commons project 下面还有 Sandbox,包含Cache ,Clazz ,Compress, Convert ,I18n,Id ,Mapper, SQL ,ThreadPool 等等,很多都用得上。哎,好东西真多,怎么介绍得完呢?

慢慢来,一个一篇文章,慢慢来。希望大家支持。



<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.5.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>exam</artifactId> <version>0.0.1-SNAPSHOT</version> <name>exam</name> <description>exam</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity6</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>6.6.11.Final</version> </dependency> <dependency> <groupId>com.networknt</groupId> <artifactId>json-schema-validator</artifactId> <version>1.5.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.16</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-core</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.2.5</version> </dependency> <!-- Bean Validation API --> <dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> <version>3.0.2</version> </dependency> <!-- Hibernate Validator (实现) --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>8.0.1.Final</version> </dependency> <!-- Spring Boot 验证支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> 检查我的Pom.xml
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值