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 弱引用的監控以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. vue-drag-chart 拖動/縮放圖表組件的實例代碼2. vue使用moment如何將時間戳轉為標準日期時間格式3. Android studio 解決logcat無過濾工具欄的操作4. 什么是Python變量作用域5. js select支持手動輸入功能實現代碼6. PHP正則表達式函數preg_replace用法實例分析7. Android Studio3.6.+ 插件搜索不到終極解決方案(圖文詳解)8. bootstrap select2 動態從后臺Ajax動態獲取數據的代碼9. Android 實現徹底退出自己APP 并殺掉所有相關的進程10. 一個 2 年 Android 開發者的 18 條忠告
