1. 程式人生 > >java開發中spring常用的工具類

java開發中spring常用的工具類

內建的resouce型別

  1. UrlResource
  2. ClassPathResource
  3. FileSystemResource
  4. ServletContextResource
  5. InputStreamResource
  6. ByteArrayResource
  7. EncodedResource 也就是Resource加上encoding, 可以認為是有編碼的資源
  8. VfsResource(在jboss裡經常用到, 相應還有 工具類 VfsUtils)
  9. org.springframework.util.xml.ResourceUtils 用於處理表達資源字串字首描述資源的工具. 如: "classpath:".

有 getURL, getFile, isFileURL, isJarURL, extractJarFileURL

工具類

  1. org.springframework.core.annotation.AnnotationUtils 處理註解
  2. org.springframework.core.io.support.PathMatchingResourcePatternResolver 用於處理 ant 匹配風格(com/.jsp, com//.jsp),找出所有的資源, 結合上面的resource的概念一起使用,對於遍歷檔案很有用. 具體請詳細檢視javadoc
  3. org.springframework.core.io.support.PropertiesLoaderUtils 載入Properties資源工具類,和Resource結合
  4. org.springframework.core.BridgeMethodResolver 橋接方法分析器. 關於橋接方法請參考: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.5
  5. org.springframework.core.GenericTypeResolver 範型分析器, 在用於對範型方法, 引數分析.
  6. 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.AntPathMatcherant風格的處理
  2. org.springframework.util.xml.AntPathStringMatcher
  3. org.springframework.util.xml.Assert斷言,在我們的引數判斷時應該經常用
  4. org.springframework.util.xml.CachingMapDecorator
  5. org.springframework.util.xml.ClassUtils用於Class的處理
  6. org.springframework.util.xml.CollectionUtils用於處理集合的工具
  7. org.springframework.util.xml.CommonsLogWriter
  8. org.springframework.util.xml.CompositeIterator
  9. org.springframework.util.xml.ConcurrencyThrottleSupport
  10. org.springframework.util.xml.CustomizableThreadCreator
  11. org.springframework.util.xml.DefaultPropertiesPersister
  12. org.springframework.util.xml.DigestUtils摘要處理, 這裡有用於md5處理資訊的
  13. org.springframework.util.xml.FileCopyUtils檔案的拷貝處理, 結合Resource的概念一起來處理, 真的是很方便
  14. org.springframework.util.xml.FileSystemUtils
  15. org.springframework.util.xml.LinkedCaseInsensitiveMap

key值不區分大小寫的LinkedMap

  1. org.springframework.util.xml.LinkedMultiValueMap一個key可以存放多個值的LinkedMap
  2. org.springframework.util.xml.Log4jConfigurer一個log4j的啟動載入指定配製檔案的工具類
  3. org.springframework.util.xml.NumberUtils處理數字的工具類, 有parseNumber 可以把字串處理成我們指定的數字格式, 還支援format格式, convertNumberToTargetClass 可以實現Number型別的轉化.
  4. org.springframework.util.xml.ObjectUtils有很多處理null object的方法. 如nullSafeHashCode, nullSafeEquals, isArray, containsElement, addObjectToArray, 等有用的方法
  5. org.springframework.util.xml.PatternMatchUtilsspring裡用於處理簡單的匹配. 如 Spring's typical "xxx", "xxx" and "xxx" pattern styles
  6. org.springframework.util.xml.PropertyPlaceholderHelper用於處理佔位符的替換
  7. org.springframework.util.xml.ReflectionUtils反映常用工具方法. 有 findField, setField, getField, findMethod, invokeMethod等有用的方法
  8. org.springframework.util.xml.SerializationUtils用於java的序列化與反序列化. serialize與deserialize方法
  9. org.springframework.util.xml.StopWatch一個很好的用於記錄執行時間的工具類, 且可以用於任務分階段的測試時間. 最後支援一個很好看的列印格式. 這個類應該經常用
  10. org.springframework.util.xml.StringUtils
  11. org.springframework.util.xml.SystemPropertyUtils
  12. org.springframework.util.xml.TypeUtils用於型別相容的判斷. isAssignable
  13. org.springframework.util.xml.WeakReferenceMonitor弱引用的監控

和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

這個類用於用字串模板構建url, 它會自動處理url裡的漢字及其它相關的編碼. 在讀取別人提供的url資源時, 應該經常用

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

  1. org.springframework.web.util.JavaScriptUtils
  2. org.springframework.web.util.Log4jConfigListener

用listener的方式來配製log4j在web環境下的初始化

  1. org.springframework.web.util.UriTemplate
  2. org.springframework.web.util.UriUtils處理uri裡特殊字元的編碼
  3. org.springframework.web.util.WebUtils
  4. org.springframework.web.util.
  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的相關處理方法。

  1. 字串工具類 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"

還有譬如陣列轉集合、集合轉陣列、路徑處理、字串分離成陣列、陣列或集合合併為字串、數組合並、向陣列新增元素等。

  1. 物件序列化與反序列化 org.springframework.util.SerializationUtils

public static byte[] serialize(Object object)

public static Object deserialize(byte[] bytes)

  1. 數字處理 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)

  1. 檔案複製 org.springframework.util.FileCopyUtils

流與流之間、流到字串、位元組陣列到流等的複製

  1. 目錄複製 org.springframework.util.FileSystemUtils

遞迴複製、刪除一個目錄

  1. MD5加密 org.springframework.util.DigestUtils

位元組陣列的MD5加密 public static String md5DigestAsHex(byte[] bytes)

為了讓學習變得輕鬆、高效,今天給大家免費分享一套Java入門教學資源。幫助大家在成為Java架構師的道路上披荊斬棘。需要入門的資料歡迎加入學習交流群:9285,05736