Spring常用一些工具類實(shí)例匯總
一、內(nèi)置Resource類型
- org.springframework.core.io.UrlResource
- org.springframework.core.io.ClassPathResource:以類路徑的方式進(jìn)行訪問(wèn)
- org.springframework.core.io.FileSystemResource:以文件系統(tǒng)絕對(duì)路徑的方式進(jìn)行訪問(wèn)
- org.springframework.web.context.support.ServletContextResource:以相對(duì)于 Web 應(yīng)用根目錄的方式進(jìn)行訪問(wèn)
- org.springframework.core.io.InputStreamResource
- org.springframework.core.io.ByteArrayResource
- org.springframework.core.io.support.EncodedResource :就是Resource加上encoding, 可以認(rèn)為是有編碼的資源。當(dāng)您使用 Resource 實(shí)現(xiàn)類加載文件資源時(shí),它默認(rèn)采用操作系統(tǒng)的編碼格式。如果文件資源采用了特殊的編碼格式(如 UTF-8),則在讀取資源內(nèi)容時(shí)必須事先通過(guò) EncodedResource 指定編碼格式,否則將會(huì)產(chǎn)生中文亂碼的問(wèn)題。
- org.springframework.core.io.VfsResource:在jboss里經(jīng)常用到, 相應(yīng)還有 工具類 VfsUtils
- org.springframework.util.ResourceUtils:它支持“classpath:”和“file:”的地址前綴,它能夠從指定的地址加載文件資源,常用方法:getFile()
二、本地化文件資源
org.springframework.core.io.support.LocalizedResourceHelper:允許通過(guò)文件資源基名和本地化實(shí)體獲取匹配的本地化文件資源并以 Resource 對(duì)象返回
三、操作 Servlet API 的工具類
org.springframework.web.context.support.WebApplicationContextUtils 工具類獲取 WebApplicationContext 對(duì)象。
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相關(guān)工具類
- org.springframework.web.util.CookieGenerator
- org.springframework.web.util.HtmlCharacterEntityDecoder
- org.springframework.web.util.HtmlCharacterEntityReferences
- org.springframework.web.util.HtmlUtils:HTML 特殊字符轉(zhuǎn)義,常用方法 htmlEscape(),htmlUnescape()。
- org.springframework.web.util.HttpUrlTemplate 這個(gè)類用于用字符串模板構(gòu)建url, 它會(huì)自動(dòng)處理url里的漢字及其它相關(guān)的編碼. 在讀取別人提供的url資源時(shí), 應(yīng)該經(jīng)常用 String url = "http://localhost/myapp/{name}/{id}"
- org.springframework.web.util.JavaScriptUtils:JavaScript 特殊字符轉(zhuǎn)義,常用方法:javaScriptEscape()。
- org.springframework.web.util.Log4jConfigListener 用listener的方式來(lái)配制log4j在web環(huán)境下的初始化
- org.springframework.web.util.UriTemplate
- org.springframework.web.util.UriUtils :處理uri里特殊字符的編碼
- org.springframework.web.util.WebUtils
- getCookie(HttpServletRequest request, String name) 獲取 HttpServletRequest 中特定名字的 Cookie 對(duì)象。如果您需要?jiǎng)?chuàng)建 Cookie, Spring 也提供了一個(gè)方便的 CookieGenerator 工具類。
- getSessionAttribute(HttpServletRequest request, String name) 獲取 HttpSession 特定屬性名的對(duì)象,否則您必須通過(guò) request.getHttpSession.getAttribute(name) 完成相同的操作。
- getRequiredSessionAttribute(HttpServletRequest request, String name) 和上一個(gè)方法類似,只不過(guò)強(qiáng)制要求 HttpSession 中擁有指定的屬性,否則拋出異常。
- getSessionId(HttpServletRequest request) 獲取 Session ID 的值。
- void exposeRequestAttributes(ServletRequest request, Map attributes) 將 Map 元素添加到 ServletRequest 的屬性列表中,當(dāng)請(qǐng)求被導(dǎo)向(forward)到下一個(gè)處理程序時(shí),這些請(qǐng)求屬性就可以被訪問(wèn)到了。
六、參數(shù)檢測(cè)工具類org.springframework.util.Assert
Assert斷言工具類,通常用于數(shù)據(jù)合法性檢查。
平時(shí)做判斷通常都是這樣寫:
if (message== null || message.equls("")) {
throw new IllegalArgumentException("輸入信息錯(cuò)誤!");
}
用Assert工具類上面的代碼可以簡(jiǎn)化為:
Assert.hasText((message, "輸入信息錯(cuò)誤!");
下面來(lái)介紹一下Assert 類中的常用斷言方法:
- Assert.notNull(Object object, "object is required") - 對(duì)象非空
- Assert.isTrue(Object object, "object must be true") - 對(duì)象必須為true
- Assert.notEmpty(Collection collection, "collection must not be empty") - 集合非空
- Assert.hasLength(String text, "text must be specified") - 字符不為null且字符長(zhǎng)度不為0
- Assert.hasText(String text, "text must not be empty") - text 不為null且必須至少包含一個(gè)非空格的字符
- Assert.isInstanceOf(Class clazz, Object obj, "clazz must be of type [clazz]") - obj必須能被正確造型成為clazz 指定的類
七、請(qǐng)求工具類 org.springframework.web.bind.ServletRequestUtils
//取請(qǐng)求參數(shù)的整數(shù)值:
public static Integer getIntParameter(ServletRequest request, String name)
public static int getIntParameter(ServletRequest request, String name, int defaultVal) -->單個(gè)值
public static int[] getIntParameters(ServletRequest request, String name) -->數(shù)組
還有譬如long、float、double、boolean、String的相關(guān)處理方法。
八、其他工具類
- org.springframework.util.FileCopyUtils:它提供了許多一步式的靜態(tài)操作方法,能夠?qū)⑽募?nèi)容拷貝到一個(gè)目標(biāo) byte[]、String 甚至一個(gè)輸出流或輸出文件中。
- org.springframework.core.io.support.PropertiesLoaderUtils:允許您直接通過(guò)基于類路徑的文件地址加載屬性資源。
- oorg.springframework.orm.hibernate5.support.OpenSessionInViewFilter:過(guò)濾器將 Hibernate Session 綁定到請(qǐng)求線程中,它將自動(dòng)被 Spring 的事務(wù)管理器探測(cè)到。所以 OpenSessionInViewFilter 適用于 Service 層使用 HibernateTransactionManager 或 JtaTransactionManager 進(jìn)行事務(wù)管理的環(huán)境,也可以用于非事務(wù)只讀的數(shù)據(jù)操作中。
- org.springframework.web.filter.CharacterEncodingFilter:當(dāng)通過(guò)表單向服務(wù)器提交數(shù)據(jù)時(shí),一個(gè)經(jīng)典的問(wèn)題就是中文亂碼問(wèn)題。雖然我們所有的 JSP 文件和頁(yè)面編碼格式都采用 UTF-8,但這個(gè)問(wèn)題還是會(huì)出現(xiàn)。解決的辦法很簡(jiǎn)單,我們只需要在 web.xml 中配置一個(gè) Spring 的編碼轉(zhuǎn)換過(guò)濾器就可以了。
- org.springframework.web.filter.ServletContextRequestLoggingFilter:請(qǐng)求跟蹤日志過(guò)濾器。在日志級(jí)別為 DEBUG 時(shí)才會(huì)起作用。
- org.springframework.web.util.WebAppRootListener
- org.springframework.web.IntrospectorCleanupListener:緩存清除監(jiān)聽(tīng)器
- org.springframework.util.StringUtils:字符串工具類
- CollectionUtils:集合工具類
- org.springframework.util.SerializationUtils:對(duì)象序列化與反序列化
- org.springframework.util.NumberUtils:處理數(shù)字的工具類, 有parseNumber 可以把字符串處理成我們指定的數(shù)字格式, 還支持format格式, convertNumberToTargetClass 可以實(shí)現(xiàn)Number類型的轉(zhuǎn)化。
- org.springframework.util.FileSystemUtils:遞歸復(fù)制、刪除一個(gè)目錄。
- org.springframework.util.DigestUtils:MD5加密
- org.springframework.util.AntPathMatcher:風(fēng)格的處理
- 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值不區(qū)分大小寫的LinkedMap
- org.springframework.util.LinkedMultiValueMap:一個(gè)key可以存放多個(gè)值的LinkedMap
- org.springframework.util.ObjectUtils:有很多處理null object的方法. 如nullSafeHashCode, nullSafeEquals, isArray, containsElement, addObjectToArray, 等有用的方法
- org.springframework.util.PatternMatchUtils:spring里用于處理簡(jiǎn)單的匹配。
- org.springframework.util.PropertyPlaceholderHelper:用于處理占位符的替換。
- org.springframework.util.ReflectionUtils:反射常用工具方法. 有 findField, setField, getField, findMethod, invokeMethod等有用的方法。
- org.springframework.util.StopWatch 一個(gè)很好的用于記錄執(zhí)行時(shí)間的工具類, 且可以用于任務(wù)分階段的測(cè)試時(shí)間. 最后支持一個(gè)很好看的打印格式. 這個(gè)類應(yīng)該經(jīng)常用。
- org.springframework.util.SystemPropertyUtils
- org.springframework.util.TypeUtils:用于類型相容的判斷. isAssignable
- org.springframework.util.WeakReferenceMonitor 弱引用的監(jiān)控
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談HBase在SpringBoot項(xiàng)目里的應(yīng)用(含HBaseUtil工具類)
- Springboot實(shí)現(xiàn)多線程注入bean的工具類操作
- Spring框架JavaMailSender發(fā)送郵件工具類詳解
- 兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil
- Spring Boot中優(yōu)雅的獲取yml文件工具類
- spring boot結(jié)合Redis實(shí)現(xiàn)工具類的方法示例
- Spring boot工具類靜態(tài)屬性注入及多環(huán)境配置詳解
- Spring獲取ApplicationContext對(duì)象工具類的實(shí)現(xiàn)方法
相關(guān)文章
Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程
這篇文章主要介紹了Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程,包括基于TCP/IP和UDP協(xié)議的簡(jiǎn)單實(shí)例程序講解,需要的朋友可以參考下2016-01-01spring boot切面execution表達(dá)式添加多個(gè)包路徑問(wèn)題及解決方案
在Spring Boot中,如果你想為多個(gè)包中的方法創(chuàng)建一個(gè)切面,你可以在@Pointcut注解中使用||操作符來(lái)指定多個(gè)包,下面給大家分享spring boot切面execution表達(dá)式添加多個(gè)包路徑問(wèn)題及解決方案,感興趣的朋友跟隨小編一起看看吧2024-03-03Java如何實(shí)現(xiàn)簡(jiǎn)單的RPC框架
這篇文章主要介紹了Java如何實(shí)現(xiàn)簡(jiǎn)單的RPC框架,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07[Java]詳解Socket和ServerSocket學(xué)習(xí)筆記
即時(shí)類應(yīng)用或者即時(shí)類的游戲,HTTP協(xié)議很多時(shí)候無(wú)法滿足于我們的需求,這會(huì),Socket對(duì)于我們來(lái)說(shuō)就非常實(shí)用了。本篇文章主要介紹了Socket和ServerSocket,有興趣的可以了解一下。2016-12-12Java 判斷兩個(gè)字符串是否由相同的字符組成的實(shí)例
今天小編就為大家分享一篇Java 判斷兩個(gè)字符串是否由相同的字符組成的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析
這篇文章主要介紹了Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析,針對(duì)用戶所使用的語(yǔ)言來(lái)配置資源文件,需要的朋友可以參考下2016-04-04log4j2 RollingRandomAccessFile配置過(guò)程
這篇文章主要介紹了log4j2 RollingRandomAccessFile配置過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07