CHARLIE SAYS

查理如是说
DATE 2026-08-24
THEME
SERIES / DEV_TOOLS / P-007 · 开发工具与库

开发工具 008:Spring常用工具类

在 Spring 框架应用中,排在 Apache Commons、Guava、Hutool 等通用库之后,第二优先级可以考虑使用 spring-core-xxx.jar 中的 util 包。提醒读者:对于工具类,要站在高一点的角度知道有哪些,而不是只看到代码,用的时候具体看下即可。

内置的 resource 类型

类型说明
UrlResourceURL 资源
ClassPathResource类路径资源
FileSystemResource文件系统资源
ServletContextResourceServlet 上下文资源
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/.jspcom//.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.AbstractStaxContentHandler
  • org.springframework.util.xml.AbstractStaxXMLReader
  • org.springframework.util.xml.AbstractXMLReader
  • org.springframework.util.xml.AbstractXMLStreamReader
  • org.springframework.util.xml.DomUtils
  • org.springframework.util.xml.SimpleNamespaceContext
  • org.springframework.util.xml.SimpleSaxErrorHandler
  • org.springframework.util.xml.SimpleTransformErrorListener
  • org.springframework.util.xml.StaxUtils
  • org.springframework.util.xml.TransformerUtils

其它工具集

工具类说明
org.springframework.util.AntPathMatcherant 风格的处理
org.springframework.util.AntPathStringMatcherant 风格字符串匹配
org.springframework.util.Assert断言,在我们的参数判断时应该经常用
org.springframework.util.CachingMapDecorator缓存 Map 装饰器
org.springframework.util.ClassUtils用于 Class 的处理
org.springframework.util.CollectionUtils用于处理集合的工具
org.springframework.util.CommonsLogWriterCommonsLog 输出
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.LinkedCaseInsensitiveMapkey 值不区分大小写的 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.PatternMatchUtilsspring 里用于处理简单的匹配,如 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.CookieGeneratorCookie 生成
org.springframework.web.util.HtmlCharacterEntityDecoderHTML 实体解码
org.springframework.web.util.HtmlCharacterEntityReferencesHTML 实体引用
org.springframework.web.util.HtmlUtilsHTML 工具
org.springframework.web.util.HttpUrlTemplate这个类用于用字符串模板构建 url,它会自动处理 url 里的汉字及其它相关的编码。在读取别人提供的 url 资源时,应该经常用,如 String url = "http://localhost/myapp/{name}/{id}"
org.springframework.web.util.JavaScriptUtilsJavaScript 工具
org.springframework.web.util.Log4jConfigListener用 listener 的方式来配制 log4j 在 web 环境下的初始化
org.springframework.web.util.UriTemplateURI 模板
org.springframework.web.util.UriUtils处理 uri 里特殊字符的编码
org.springframework.web.util.WebUtilsWeb 工具

系列导航

← 开发安全 008:加密算法 目录 算法 009:插入排序(Insertion Sort) →
← 返回文章列表