在 Spring 框架应用中,排在 Apache Commons、Guava、Hutool 等通用库之后,第二优先级可以考虑使用 spring-core-xxx.jar 中的 util 包。提醒读者:对于工具类,要站在高一点的角度知道有哪些,而不是只看到代码,用的时候具体看下即可。
内置的 resource 类型
| 类型 | 说明 |
|---|---|
| UrlResource | URL 资源 |
| ClassPathResource | 类路径资源 |
| FileSystemResource | 文件系统资源 |
| ServletContextResource | Servlet 上下文资源 |
| InputStreamResource | 输入流资源 |
| ByteArrayResource | 字节数组资源 |
| EncodedResource | 也就是 Resource 加上 encoding,可以认为是有编码的资源 |
| VfsResource | 在 jboss 里经常用到,相应还有工具类 VfsUtils |
org.springframework.util.xml.ResourceUtils 用于处理表达资源字符串前缀描述资源的工具,如 "classpath:",有 getURL、getFile、isFileURL、isJarURL、extractJarFileURL 等方法。
工具类
| 工具类 | 说明 |
|---|---|
org.springframework.core.annotation.AnnotationUtils | 处理注解 |
org.springframework.core.io.support.PathMatchingResourcePatternResolver | 用于处理 ant 匹配风格(com/.jsp、com//.jsp),找出所有的资源,结合上面的 resource 的概念一起使用,对于遍历文件很有用,具体请详细查看 javadoc |
org.springframework.core.io.support.PropertiesLoaderUtils | 加载 Properties 资源工具类,和 Resource 结合 |
org.springframework.core.BridgeMethodResolver | 桥接方法分析器。关于桥接方法请参考 JLS:http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.5 |
org.springframework.core.GenericTypeResolver | 范型分析器,在用于对范型方法、参数分析 |
org.springframework.core.NestedExceptionUtils | 嵌套异常工具 |
xml 工具
org.springframework.util.xml.AbstractStaxContentHandlerorg.springframework.util.xml.AbstractStaxXMLReaderorg.springframework.util.xml.AbstractXMLReaderorg.springframework.util.xml.AbstractXMLStreamReaderorg.springframework.util.xml.DomUtilsorg.springframework.util.xml.SimpleNamespaceContextorg.springframework.util.xml.SimpleSaxErrorHandlerorg.springframework.util.xml.SimpleTransformErrorListenerorg.springframework.util.xml.StaxUtilsorg.springframework.util.xml.TransformerUtils
其它工具集
| 工具类 | 说明 |
|---|---|
org.springframework.util.AntPathMatcher | ant 风格的处理 |
org.springframework.util.AntPathStringMatcher | ant 风格字符串匹配 |
org.springframework.util.Assert | 断言,在我们的参数判断时应该经常用 |
org.springframework.util.CachingMapDecorator | 缓存 Map 装饰器 |
org.springframework.util.ClassUtils | 用于 Class 的处理 |
org.springframework.util.CollectionUtils | 用于处理集合的工具 |
org.springframework.util.CommonsLogWriter | CommonsLog 输出 |
org.springframework.util.CompositeIterator | 组合迭代器 |
org.springframework.util.ConcurrencyThrottleSupport | 并发节流支持 |
org.springframework.util.CustomizableThreadCreator | 可定制线程创建者 |
org.springframework.util.DefaultPropertiesPersister | 默认 Properties 持久化 |
org.springframework.util.DigestUtils | 摘要处理,这里有用于 md5 处理信息的 |
org.springframework.util.FileCopyUtils | 文件的拷贝处理,结合 Resource 的概念一起来处理,真的是很方便 |
org.springframework.util.FileSystemUtils | 文件系统工具 |
org.springframework.util.LinkedCaseInsensitiveMap | key 值不区分大小写的 LinkedMap |
org.springframework.util.LinkedMultiValueMap | 一个 key 可以存放多个值的 LinkedMap |
org.springframework.util.Log4jConfigurer | 一个 log4j 的启动加载指定配制文件的工具类 |
org.springframework.util.NumberUtils | 处理数字的工具类,有 parseNumber 可以把字符串处理成我们指定的数字格式,还支持 format 格式,convertNumberToTargetClass 可以实现 Number 类型的转化 |
org.springframework.util.ObjectUtils | 有很多处理 null object 的方法,如 nullSafeHashCode、nullSafeEquals、isArray、containsElement、addObjectToArray 等有用的方法 |
org.springframework.util.PatternMatchUtils | spring 里用于处理简单的匹配,如 Spring’s typical "xxx*"、"*xxx" and "*xxx*" pattern styles |
org.springframework.util.PropertyPlaceholderHelper | 用于处理占位符的替换 |
org.springframework.util.ReflectionUtils | 反射常用工具方法,有 findField、setField、getField、findMethod、invokeMethod 等有用的方法 |
org.springframework.util.SerializationUtils | 用于 java 的序列化与反序列化,serialize 与 deserialize 方法 |
org.springframework.util.StopWatch | 一个很好的用于记录执行时间的工具类,且可以用于任务分阶段的测试时间,最后支持一个很好看的打印格式,这个类应该经常用 |
org.springframework.util.StringUtils | 字符串工具 |
org.springframework.util.SystemPropertyUtils | 系统属性工具 |
org.springframework.util.TypeUtils | 用于类型相容的判断,isAssignable |
org.springframework.util.WeakReferenceMonitor | 弱引用的监控 |
和 web 相关的工具
| 工具类 | 说明 |
|---|---|
org.springframework.web.util.CookieGenerator | Cookie 生成 |
org.springframework.web.util.HtmlCharacterEntityDecoder | HTML 实体解码 |
org.springframework.web.util.HtmlCharacterEntityReferences | HTML 实体引用 |
org.springframework.web.util.HtmlUtils | HTML 工具 |
org.springframework.web.util.HttpUrlTemplate | 这个类用于用字符串模板构建 url,它会自动处理 url 里的汉字及其它相关的编码。在读取别人提供的 url 资源时,应该经常用,如 String url = "http://localhost/myapp/{name}/{id}" |
org.springframework.web.util.JavaScriptUtils | JavaScript 工具 |
org.springframework.web.util.Log4jConfigListener | 用 listener 的方式来配制 log4j 在 web 环境下的初始化 |
org.springframework.web.util.UriTemplate | URI 模板 |
org.springframework.web.util.UriUtils | 处理 uri 里特殊字符的编码 |
org.springframework.web.util.WebUtils | Web 工具 |