1. 程式人生 > 程式設計 >Spring常用一些工具類例項彙總

Spring常用一些工具類例項彙總

一、內建Resource型別

  • org.springframework.core.io.UrlResource
  • org.springframework.core.io.ClassPathResource:以類路徑的方式進行訪問
  • org.springframework.core.io.FileSystemResource:以檔案系統絕對路徑的方式進行訪問
  • org.springframework.web.context.support.ServletContextResource:以相對於 Web 應用根目錄的方式進行訪問
  • org.springframework.core.io.InputStreamResource
  • org.springframework.core.io.ByteArrayResource
  • org.springframework.core.io.support.EncodedResource :就是Resource加上encoding,可以認為是有編碼的資源。當您使用 Resource 實現類載入檔案資源時,它預設採用作業系統的編碼格式。如果檔案資源採用了特殊的編碼格式(如 UTF-8),則在讀取資源內容時必須事先通過 EncodedResource 指定編碼格式,否則將會產生中文亂碼的問題。
  • org.springframework.core.io.VfsResource:在jboss裡經常用到,相應還有 工具類 VfsUtils
  • org.springframework.util.ResourceUtils:它支援“classpath:”和“file:”的地址字首,它能夠從指定的地址載入檔案資源,常用方法:getFile()

二、本地化檔案資源

org.springframework.core.io.support.LocalizedResourceHelper:允許通過檔案資源基名和本地化實體獲取匹配的本地化檔案資源並以 Resource 物件返回

三、操作 Servlet API 的工具類

org.springframework.web.context.support.WebApplicationContextUtils 工具類獲取 WebApplicationContext 物件。

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);

四、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

五、web相關工具類

  • org.springframework.web.util.CookieGenerator
  • org.springframework.web.util.HtmlCharacterEntityDecoder
  • org.springframework.web.util.HtmlCharacterEntityReferences
  • org.springframework.web.util.HtmlUtils:HTML 特殊字元轉義,常用方法 htmlEscape(),htmlUnescape()。
  • org.springframework.web.util.HttpUrlTemplate 這個類用於用字串模板構建url,它會自動處理url裡的漢字及其它相關的編碼. 在讀取別人提供的url資源時,應該經常用 String url = "http://localhost/myapp/{name}/{id}"
  • org.springframework.web.util.JavaScriptUtils:JavaScript 特殊字元轉義,常用方法:javaScriptEscape()。
  • org.springframework.web.util.Log4jConfigListener 用listener的方式來配製log4j在web環境下的初始化
  • org.springframework.web.util.UriTemplate
  • org.springframework.web.util.UriUtils :處理uri裡特殊字元的編碼
  • org.springframework.web.util.WebUtils
  • getCookie(HttpServletRequest request,String name) 獲取 HttpServletRequest 中特定名字的 Cookie 物件。如果您需要建立 Cookie, Spring 也提供了一個方便的 CookieGenerator 工具類。
  • getSessionAttribute(HttpServletRequest request,String name) 獲取 HttpSession 特定屬性名的物件,否則您必須通過 request.getHttpSession.getAttribute(name) 完成相同的操作。
  • getRequiredSessionAttribute(HttpServletRequest request,String name) 和上一個方法類似,只不過強制要求 HttpSession 中擁有指定的屬性,否則丟擲異常。
  • getSessionId(HttpServletRequest request) 獲取 Session ID 的值。
  • void exposeRequestAttributes(ServletRequest request,Map attributes) 將 Map 元素新增到 ServletRequest 的屬性列表中,當請求被導向(forward)到下一個處理程式時,這些請求屬性就可以被訪問到了。

六、引數檢測工具類org.springframework.util.Assert

Assert斷言工具類,通常用於資料合法性檢查。

平時做判斷通常都是這樣寫:

if (message== null || message.equls("")) {
throw new IllegalArgumentException("輸入資訊錯誤!");
}

用Assert工具類上面的程式碼可以簡化為:

Assert.hasText((message,"輸入資訊錯誤!");
下面來介紹一下Assert 類中的常用斷言方法:

  • Assert.notNull(Object object,"object is required") - 物件非空
  • Assert.isTrue(Object object,"object must be true") - 物件必須為true
  • Assert.notEmpty(Collection collection,"collection must not be empty") - 集合非空
  • Assert.hasLength(String text,"text must be specified") - 字元不為null且字元長度不為0
  • Assert.hasText(String text,"text must not be empty") - text 不為null且必須至少包含一個非空格的字元
  • Assert.isInstanceOf(Class clazz,Object obj,"clazz must be of type [clazz]") - obj必須能被正確造型成為clazz 指定的類

七、請求工具類 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的相關處理方法。

八、其他工具類

  • org.springframework.util.FileCopyUtils:它提供了許多一步式的靜態操作方法,能夠將檔案內容拷貝到一個目標 byte[]、String 甚至一個輸出流或輸出檔案中。
  • org.springframework.core.io.support.PropertiesLoaderUtils:允許您直接通過基於類路徑的檔案地址載入屬性資源。
  • oorg.springframework.orm.hibernate5.support.OpenSessionInViewFilter:過濾器將 Hibernate Session 繫結到請求執行緒中,它將自動被 Spring 的事務管理器探測到。所以 OpenSessionInViewFilter 適用於 Service 層使用 HibernateTransactionManager 或 JtaTransactionManager 進行事務管理的環境,也可以用於非事務只讀的資料操作中。
  • org.springframework.web.filter.CharacterEncodingFilter:當通過表單向伺服器提交資料時,一個經典的問題就是中文亂碼問題。雖然我們所有的 JSP 檔案和頁面編碼格式都採用 UTF-8,但這個問題還是會出現。解決的辦法很簡單,我們只需要在 web.xml 中配置一個 Spring 的編碼轉換過濾器就可以了。
  • org.springframework.web.filter.ServletContextRequestLoggingFilter:請求跟蹤日誌過濾器。在日誌級別為 DEBUG 時才會起作用。
  • org.springframework.web.util.WebAppRootListener
  • org.springframework.web.IntrospectorCleanupListener:快取清除監聽器
  • org.springframework.util.StringUtils:字串工具類
  • CollectionUtils:集合工具類
  • org.springframework.util.SerializationUtils:物件序列化與反序列化
  • org.springframework.util.NumberUtils:處理數字的工具類,有parseNumber 可以把字串處理成我們指定的數字格式,還支援format格式,convertNumberToTargetClass 可以實現Number型別的轉化。
  • org.springframework.util.FileSystemUtils:遞迴複製、刪除一個目錄。
  • org.springframework.util.DigestUtils:MD5加密
  • org.springframework.util.AntPathMatcher:風格的處理
  • org.springframework.util.AntPathStringMatcher
  • org.springframework.util.ClassUtils:用於Class的處理
  • org.springframework.util.CommonsLogWriter
  • org.springframework.util.CompositeIterator
  • org.springframework.util.ConcurrencyThrottleSupport
  • org.springframework.util.CustomizableThreadCreator
  • org.springframework.util.DefaultPropertiesPersister
  • org.springframework.util.LinkedCaseInsensitiveMap:key值不區分大小寫的LinkedMap
  • org.springframework.util.LinkedMultiValueMap:一個key可以存放多個值的LinkedMap
  • org.springframework.util.ObjectUtils:有很多處理null object的方法. 如nullSafeHashCode,nullSafeEquals,isArray,containsElement,addObjectToArray,等有用的方法
  • org.springframework.util.PatternMatchUtils:spring裡用於處理簡單的匹配。
  • org.springframework.util.PropertyPlaceholderHelper:用於處理佔位符的替換。
  • org.springframework.util.ReflectionUtils:反射常用工具方法. 有 findField,setField,getField,findMethod,invokeMethod等有用的方法。
  • org.springframework.util.StopWatch 一個很好的用於記錄執行時間的工具類,且可以用於任務分階段的測試時間. 最後支援一個很好看的列印格式. 這個類應該經常用。
  • org.springframework.util.SystemPropertyUtils
  • org.springframework.util.TypeUtils:用於型別相容的判斷. isAssignable
  • org.springframework.util.WeakReferenceMonitor 弱引用的監控

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。