spring里的工具类,摘自spring3.0.5版本

转载:http://wen66.iteye.com/blog/830719

spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 

而不想把一个大大的spring工程给引入进来. 下面是我从spring3.0.5里抽取出来的工具类. 

在最后给出我提取出来的spring代码打成的jar包 

spring的里的resouce的概念, 在我们处理io时很有用. 具体信息请参考spring手册 

内置的resouce类型 

  1. UrlResource

  2. ClassPathResource

  3. FileSystemResource

  4. ServletContextResource

  5. InputStreamResource

  6. ByteArrayResource

  7. EncodedResource 

  8. 也就是Resource加上encoding, 可以认为是有编码的资源

  9. VfsResource

  10. (在jboss里经常用到, 相应还有 工具类 VfsUtils)

  11. org.springframework.util.xml.ResourceUtils 

  12. 用于处理表达资源字符串前缀描述资源的工具. 如: "classpath:". 


  13. 有 getURL, getFile, isFileURL, isJarURL, extractJarFileURL 




工具类  

  1. org.springframework.core.annotation.AnnotationUtils   

  2. 处理注解

  3. org.springframework.core.io.support.PathMatchingResourcePatternResolver  

  4. 用于处理 ant 匹配风格(com/*.jsp, com/**/*.jsp),找出所有的资源, 结合上面的resource的概念一起使用,对于遍历文件很有用. 具体请详细查看javadoc

  5. org.springframework.core.io.support.PropertiesLoaderUtils 

  6. 加载Properties资源工具类,和Resource结合

  7. org.springframework.core.BridgeMethodResolver  

  8. 桥接方法分析器.  关于桥接方法请参考: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.5

  9. org.springframework.core.GenericTypeResolver  

  10. 范型分析器, 在用于对范型方法, 参数分析.

  11. org.springframework.core.NestedExceptionUtils



xml工具  

  1. org.springframework.util.xml.AbstractStaxContentHandler

  2. org.springframework.util.xml.AbstractStaxXMLReader

  3. org.springframework.util.xml.AbstractXMLReader

  4. org.springframework.util.xml.AbstractXMLStreamReader

  5. org.springframework.util.xml.DomUtils

  6. org.springframework.util.xml.SimpleNamespaceContext

  7. org.springframework.util.xml.SimpleSaxErrorHandler

  8. org.springframework.util.xml.SimpleTransformErrorListener

  9. org.springframework.util.xml.StaxUtils

  10. org.springframework.util.xml.TransformerUtils




其它工具集  

  1. org.springframework.util.xml.AntPathMatcher

  2. ant风格的处理

  3. org.springframework.util.xml.AntPathStringMatcher

  4. org.springframework.util.xml.Assert

  5. 断言,在我们的参数判断时应该经常用

  6. org.springframework.util.xml.CachingMapDecorator

  7. org.springframework.util.xml.ClassUtils

  8. 用于Class的处理

  9. org.springframework.util.xml.CollectionUtils

  10. 用于处理集合的工具

  11. org.springframework.util.xml.CommonsLogWriter

  12. org.springframework.util.xml.CompositeIterator

  13. org.springframework.util.xml.ConcurrencyThrottleSupport

  14. org.springframework.util.xml.CustomizableThreadCreator

  15. org.springframework.util.xml.DefaultPropertiesPersister

  16. org.springframework.util.xml.DigestUtils

  17. 摘要处理, 这里有用于md5处理信息的

  18. org.springframework.util.xml.FileCopyUtils

  19. 文件的拷贝处理, 结合Resource的概念一起来处理, 真的是很方便

  20. org.springframework.util.xml.FileSystemUtils

  21. org.springframework.util.xml.LinkedCaseInsensitiveMap


  22. key值不区分大小写的LinkedMap

  23. org.springframework.util.xml.LinkedMultiValueMap

  24. 一个key可以存放多个值的LinkedMap

  25. org.springframework.util.xml.Log4jConfigurer

  26. 一个log4j的启动加载指定配制文件的工具类

  27. org.springframework.util.xml.NumberUtils

  28. 处理数字的工具类, 有parseNumber 可以把字符串处理成我们指定的数字格式, 还支持format格式, convertNumberToTargetClass 可以实现Number类型的转化. 


  29. org.springframework.util.xml.ObjectUtils

  30. 有很多处理null object的方法. 如nullSafeHashCode, nullSafeEquals, isArray, containsElement, addObjectToArray, 等有用的方法

  31. org.springframework.util.xml.PatternMatchUtils

  32. spring里用于处理简单的匹配. 如 Spring's typical "xxx*", "*xxx" and "*xxx*" pattern styles

  33. org.springframework.util.xml.PropertyPlaceholderHelper

  34. 用于处理占位符的替换

  35. org.springframework.util.xml.ReflectionUtils

  36. 反映常用工具方法. 有 findField, setField, getField, findMethod, invokeMethod等有用的方法

  37. org.springframework.util.xml.SerializationUtils

  38. 用于java的序列化与反序列化. serialize与deserialize方法

  39. org.springframework.util.xml.StopWatch

  40. 一个很好的用于记录执行时间的工具类, 且可以用于任务分阶段的测试时间. 最后支持一个很好看的打印格式. 这个类应该经常用

  41. org.springframework.util.xml.StringUtils

  42. org.springframework.util.xml.SystemPropertyUtils

  43. org.springframework.util.xml.TypeUtils

  44. 用于类型相容的判断. isAssignable

  45. org.springframework.util.xml.WeakReferenceMonitor

  46. 弱引用的监控 




和web相关的工具  

  1. org.springframework.web.util.CookieGenerator

  2. org.springframework.web.util.HtmlCharacterEntityDecoder

  3. org.springframework.web.util.HtmlCharacterEntityReferences

  4. org.springframework.web.util.HtmlUtils

  5. org.springframework.web.util.HttpUrlTemplate


  6. 这个类用于用字符串模板构建url, 它会自动处理url里的汉字及其它相关的编码. 在读取别人提供的url资源时, 应该经常用 


  7. String url = "http://localhost/myapp/{name}/{id}"

  8. org.springframework.web.util.JavaScriptUtils

  9. org.springframework.web.util.Log4jConfigListener


  10. 用listener的方式来配制log4j在web环境下的初始化

  11. org.springframework.web.util.UriTemplate

  12. org.springframework.web.util.UriUtils

  13. 处理uri里特殊字符的编码

  14. org.springframework.web.util.WebUtils  

Spring3中好用的工具类收

------------------------------

1) 请求工具类 org.springframework.web.bind.ServletRequestUtils

//取请求参数的整数值:

public static Integer getIntParameter(ServletRequest request, String name)

public static int getIntParameter(ServletRequest request, String name, int defaultVal) --> 单个值

public static int[] getIntParameters(ServletRequest request, String name) --> 数组

还有譬如 long、float、double、boolean、String的相关处理方法。


2) 字符串工具类 org.springframework.util.StringUtils

首字母大写: public static String capitalize(String str)

首字母小写:public static String uncapitalize(String str)

判断字符串是否为null或empty: public static boolean hasLength(String str)

判断字符串是否为非空白字符串(即至少包含一个非空格的字符串):public static boolean hasText(String str)

获取文件名:public static String getFilename(String path) 如e.g. "mypath/myfile.txt" -> "myfile.txt"

获取文件扩展名:public static String getFilenameExtension(String path) 如"mypath/myfile.txt" -> "txt"


还有譬如数组转集合、集合转数组、路径处理、字符串分离成数组、数组或集合合并为字符串、数组合并、向数组添加元素等。


3) 对象序列化与反序列化 org.springframework.util.SerializationUtils

public static byte[] serialize(Object object)

public static Object deserialize(byte[] bytes)


4) 数字处理 org.springframework.util.NumberUtils

字符串转换为Number并格式化,包括具体的Number实现类,如Long、Integer、Double,字符串支持16进制字符串,并且会自动去除字符串中的空格:

    public static <T extends Number> T parseNumber(String text, Class<T> targetClass)

    public static <T extends Number> T parseNumber(String text, Class<T> targetClass,  NumberFormatnumberFormat)

各种Number中的转换,如Long专为Integer,自动处理数字溢出(抛出异常):

public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)


5) 文件复制 org.springframework.util.FileCopyUtils

流与流之间、流到字符串、字节数组到流等的复制


6) 目录复制 org.springframework.util.FileSystemUtils

递归复制、删除一个目录


7) MD5加密 org.springframework.util.DigestUtils

字节数组的MD5加密 public static String md5DigestAsHex(byte[] bytes)

转载于:https://my.oschina.net/frankies/blog/96025

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值