欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)單了解Spring中常用工具類

 更新時(shí)間:2017年10月10日 16:20:09   作者:harry_ma  
這篇文章主要介紹了簡(jiǎn)單了解Spring中常用工具類,非常全面,具有一定參考價(jià)值,需要的朋友可以了解下。

文件資源操作

Spring 定義了一個(gè) org.springframework.core.io.Resource 接口,Resource 接口是為了統(tǒng)一各種類型不同的資源而定義的,Spring 提供了若干 Resource 接口的實(shí)現(xiàn)類,這些實(shí)現(xiàn)類可以輕松地加載不同類型的底層資源,并提供了獲取文件名、URL 地址以及資源內(nèi)容的操作方法

訪問文件資源

* 通過 FileSystemResource 以文件系統(tǒng)絕對(duì)路徑的方式進(jìn)行訪問;

* 通過 ClassPathResource 以類路徑的方式進(jìn)行訪問;

* 通過 ServletContextResource 以相對(duì)于 Web 應(yīng)用根目錄的方式進(jìn)行訪問。

package com.baobaotao.io; 
import java.io.IOException; 
import java.io.InputStream; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.FileSystemResource; 
import org.springframework.core.io.Resource; 
public class FileSourceExample { 
public static void main(String[] args) { 
try { 
String filePath = 
"D:/masterSpring/chapter23/webapp/WEB-INF/classes/conf/file1.txt"; 
// ① 使用系統(tǒng)文件路徑方式加載文件
Resource res1 = new FileSystemResource(filePath); 
// ② 使用類路徑方式加載文件
Resource res2 = new ClassPathResource("conf/file1.txt"); 
InputStream ins1 = res1.getInputStream(); 
InputStream ins2 = res2.getInputStream(); 
System.out.println("res1:"+res1.getFilename()); 
System.out.println("res2:"+res2.getFilename()); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
}

在獲取資源后,您就可以通過 Resource 接口定義的多個(gè)方法訪問文件的數(shù)據(jù)和其它的信息

getFileName() 獲取文件名,

getFile() 獲取資源對(duì)應(yīng)的 File 對(duì)象,

getInputStream() 直接獲取文件的輸入流。

createRelative(String relativePath) 在資源相對(duì)地址上創(chuàng)建新的資源。

在 Web 應(yīng)用中,您還可以通過 ServletContextResource 以相對(duì)于 Web 應(yīng)用根目錄的方式訪問文件資源

Spring 提供了一個(gè) ResourceUtils 工具類,它支持“classpath:”和“file:”的地址前綴 ,它能夠從指定的地址加載文件資源。

File clsFile = ResourceUtils.getFile("classpath:conf/file1.txt"); 
System.out.println(clsFile.isFile()); 
String httpFilePath = "file:D:/masterSpring/chapter23/src/conf/file1.txt"; 
File httpFile = ResourceUtils.getFile(httpFilePath);

文件操作

在使用各種 Resource 接口的實(shí)現(xiàn)類加載文件資源后,經(jīng)常需要對(duì)文件資源進(jìn)行讀取、拷貝、轉(zhuǎn)存等不同類型的操作。

FileCopyUtils

它提供了許多一步式的靜態(tài)操作方法,能夠?qū)⑽募?nèi)容拷貝到一個(gè)目標(biāo) byte[]、String 甚至一個(gè)輸出流或輸出文件中。

package com.baobaotao.io; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileReader; 
import java.io.OutputStream; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 
import org.springframework.util.FileCopyUtils; 
public class FileCopyUtilsExample { 
public static void main(String[] args) throws Throwable { 
Resource res = new ClassPathResource("conf/file1.txt"); 
// ① 將文件內(nèi)容拷貝到一個(gè) byte[] 中
byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile()); 
// ② 將文件內(nèi)容拷貝到一個(gè) String 中
String fileStr = FileCopyUtils.copyToString(new FileReader(res.getFile())); 
// ③ 將文件內(nèi)容拷貝到另一個(gè)目標(biāo)文件
FileCopyUtils.copy(res.getFile(), 
new File(res.getFile().getParent()+ "/file2.txt")); 
// ④ 將文件內(nèi)容拷貝到一個(gè)輸出流中
OutputStream os = new ByteArrayOutputStream(); 
FileCopyUtils.copy(res.getInputStream(), os); 
} 
} 

static void copy(byte[] in, File out) 將 byte[] 拷貝到一個(gè)文件中

static void copy(byte[] in, OutputStream out) 將 byte[] 拷貝到一個(gè)輸出流中

static int copy(File in, File out) 將文件拷貝到另一個(gè)文件中

static int copy(InputStream in, OutputStream out) 將輸入流拷貝到輸出流中

static int copy(Reader in, Writer out) 將 Reader 讀取的內(nèi)容拷貝到 Writer 指向目標(biāo)輸出中

static void copy(String in, Writer out) 將字符串拷貝到一個(gè) Writer 指向的目標(biāo)中

屬性文件操作

Spring 提供的 PropertiesLoaderUtils 允許您直接通過基于類路徑的文件 地址加載屬性資源

package com.baobaotao.io; 
import java.util.Properties; 
import org.springframework.core.io.support.PropertiesLoaderUtils; 
public class PropertiesLoaderUtilsExample { 
public static void main(String[] args) throws Throwable { 
// ① jdbc.properties 是位于類路徑下的文件 
Properties props = PropertiesLoaderUtils.loadAllProperties("jdbc.properties"); 
System.out.println(props.getProperty("jdbc.driverClassName")); 
} 
}

特殊編碼的資源

當(dāng)您使用 Resource 實(shí)現(xiàn)類加載文件資源時(shí),它默認(rèn)采用操作系統(tǒng)的編碼格式。如果文件資源采用了特殊的編碼格式(如UTF-8),則在讀取資源內(nèi)容時(shí)必須事先通過 EncodedResource 指定編碼格式,否則將會(huì)產(chǎn)生中文亂碼的問題。

package com.baobaotao.io; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 
import org.springframework.core.io.support.EncodedResource; 
import org.springframework.util.FileCopyUtils; 
public class EncodedResourceExample { 
public static void main(String[] args) throws Throwable { 
Resource res = new ClassPathResource("conf/file1.txt"); 
// ① 指定文件資源對(duì)應(yīng)的編碼格式(UTF-8)
EncodedResource encRes = new EncodedResource(res,"UTF-8"); 
// ② 這樣才能正確讀取文件的內(nèi)容,而不會(huì)出現(xiàn)亂碼
String content = FileCopyUtils.copyToString(encRes.getReader()); 
System.out.println(content); 
} 
}

訪問 Spring 容器,獲取容器中的 Bean,使用 WebApplicationContextUtils 工具類

ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext wac = WebApplicationContextUtils. getWebApplicationContext(servletContext);

Spring 所提供的過濾器和監(jiān)聽器

延遲加載過濾器

Hibernate 允許對(duì)關(guān)聯(lián)對(duì)象、屬性進(jìn)行延遲加載,但是必須保證延遲加載的操作限于同一個(gè) Hibernate Session 范圍之內(nèi)進(jìn)行。如果 Service 層返回一個(gè)啟用了延遲加載功能的領(lǐng)域?qū)ο蠼o Web 層,當(dāng) Web 層訪問到那些需要延遲加載的數(shù)據(jù)時(shí),由于加載領(lǐng)域?qū)ο蟮?Hibernate Session 已經(jīng)關(guān)閉,這些導(dǎo)致延遲加載數(shù)據(jù)的訪問異常。

Spring 為此專門提供了一個(gè) OpenSessionInViewFilter 過濾器,它的主要功能是使每個(gè)請(qǐng)求過程綁定一個(gè) HibernateSession,即使最初的事務(wù)已經(jīng)完成了,也可以在 Web 層進(jìn)行延遲加載的操作。

<filter> 
<filter-name>hibernateFilter</filter-name> 
<filter-class> 
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
</filter-class> 
</filter> 
<filter-mapping> 
<filter-name>hibernateFilter</filter-name> 
<url-pattern>*.html</url-pattern> 
</filter-mapping>

中文亂碼過濾器

<filter> 
<filter-name>encodingFilter</filter-name> 
<filter-class> 
org.springframework.web.filter.CharacterEncodingFilter ① Spring 編輯過濾器
</filter-class> 
<init-param> ② 編碼方式
<param-name>encoding</param-name> 
<param-value>UTF-8</param-value> 
</init-param> 
<init-param> ③ 強(qiáng)制進(jìn)行編碼轉(zhuǎn)換
<param-name>forceEncoding</param-name> 
<param-value>true</param-value> 
</init-param> 
</filter> 
<filter-mapping> ② 過濾器的匹配 URL 
<filter-name>encodingFilter</filter-name> 
<url-pattern>*.html</url-pattern> 
</filter-mapping>

一般情況下,您必須將 Log4J 日志配置文件以 log4j.properties 為文件名并保存在類路徑下。Log4jConfigListener

允許您通過 log4jConfigLocation Servlet 上下文參數(shù)顯式指定 Log4J 配置文件的地址,如下所示:

① 指定 Log4J 配置文件的地址

<context-param> 
<param-name>log4jConfigLocation</param-name> 
<param-value>/WEB-INF/log4j.properties</param-value> 
</context-param> 

② 使用該監(jiān)聽器初始化 Log4J 日志引擎

<listener> 
<listener-class> 
org.springframework.web.util.Log4jConfigListener 
</listener-class> 
</listener>
Introspector 緩存清除監(jiān)聽器,防止內(nèi)存泄露
<listener> 
<listener-class> 
org.springframework.web.util.IntrospectorCleanupListener 
</listener-class> 
</listener>

一些 Web 應(yīng)用服務(wù)器(如 Tomcat)不會(huì)為不同的 Web 應(yīng)用使用獨(dú)立的系統(tǒng)參數(shù),也就是說,應(yīng)用服務(wù)器上所有的 Web 應(yīng)用都共享同一個(gè)系統(tǒng)參數(shù)對(duì)象。這時(shí),您必須通過 webAppRootKey 上下文參數(shù)為不同 Web 應(yīng)用指定不同的屬性名:如第一個(gè) Web 應(yīng)用使用 webapp1.root 而第二個(gè) Web 應(yīng)用使用 webapp2.root 等,這樣才不會(huì)發(fā)生后者覆蓋前者的問題。此外,WebAppRootListener 和 Log4jConfigListener 都只能應(yīng)用在 Web 應(yīng)用部署后 WAR 文件會(huì)解包的 Web 應(yīng)用服務(wù)器上。一些 Web 應(yīng)用服務(wù)器不會(huì)將 Web 應(yīng)用的 WAR 文件解包,整個(gè) Web 應(yīng)用以一個(gè) WAR 包的方式存在(如 Weblogic),此時(shí)因?yàn)闊o法指定對(duì)應(yīng)文件系統(tǒng)的 Web 應(yīng)用根目錄,使用這兩個(gè)監(jiān)聽器將會(huì)發(fā)生問題。

特殊字符轉(zhuǎn)義

Web 開發(fā)者最常面對(duì)需要轉(zhuǎn)義的特殊字符類型:
* HTML 特殊字符;
* JavaScript 特殊字符;
HTML 特殊字符轉(zhuǎn)義
* & :&amp;
* " :&quot;
* < :&lt;
* > :&gt;

JavaScript 特殊字符轉(zhuǎn)義

* ' :/'
* " :/"
* / ://
* 走紙換頁(yè): /f
* 換行:/n
* 換欄符:/t
* 回車:/r
* 回退符:/b

工具類

JavaScriptUtils.javaScriptEscape(String str);轉(zhuǎn)換為javaScript轉(zhuǎn)義字符表示
HtmlUtils.htmlEscape(String str);①轉(zhuǎn)換為HTML轉(zhuǎn)義字符表示
HtmlUtils.htmlEscapeDecimal(String str); ②轉(zhuǎn)換為數(shù)據(jù)轉(zhuǎn)義表示
HtmlUtils.htmlEscapeHex(String str); ③轉(zhuǎn)換為十六進(jìn)制數(shù)據(jù)轉(zhuǎn)義表示
HtmlUtils.htmlUnescape(String str);將經(jīng)過轉(zhuǎn)義內(nèi)容還原

Spring給我們提供了很多的工具類, 應(yīng)該在我們的日常工作中很好的利用起來. 它可以大大的減輕我們的平時(shí)編寫代碼的長(zhǎng)度. 因我們只想用spring的工具類,

而不想把一個(gè)大大的spring工程給引入進(jìn)來. 下面是我從spring3.0.5里抽取出來的工具類.

在最后給出我提取出來的spring代碼打成的jar包

spring的里的resouce的概念, 在我們處理io時(shí)很有用. 具體信息請(qǐng)參考spring手冊(cè)

內(nèi)置的resouce類型

UrlResource
ClassPathResource
FileSystemResource
ServletContextResource
InputStreamResource
ByteArrayResource

EncodedResource 也就是Resource加上encoding, 可以認(rèn)為是有編碼的資源

VfsResource(在jboss里經(jīng)常用到, 相應(yīng)還有 工具類 VfsUtils)

org.springframework.util.xml.ResourceUtils 用于處理表達(dá)資源字符串前綴描述資源的工具. 如: &quot;classpath:&quot;.
有 getURL, getFile, isFileURL, isJarURL, extractJarFileURL

工具類

org.springframework.core.annotation.AnnotationUtils 處理注解
org.springframework.core.io.support.PathMatchingResourcePatternResolver 用 于處理 ant 匹配風(fēng)(com/*.jsp,com/**/*.jsp),找出所有的資源, 結(jié)合上面的resource的概念一起使用,對(duì)于遍歷文件很有用. 具體請(qǐng)?jiān)敿?xì)查看javadoc
org.springframework.core.io.support.PropertiesLoaderUtils 加載Properties資源工具類,和Resource結(jié)合
org.springframework.core.BridgeMethodResolver 橋接方法分析器. 關(guān)于橋接方法請(qǐng)參考:http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.5
org.springframework.core.GenericTypeResolver 范型分析器, 在用于對(duì)范型方法, 參數(shù)分析.
org.springframework.core.NestedExceptionUtils

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

其它工具集

org.springframework.util.xml.AntPathMatcherant風(fēng)格的處理
org.springframework.util.xml.AntPathStringMatcher
org.springframework.util.xml.Assert斷言,在我們的參數(shù)判斷時(shí)應(yīng)該經(jīng)常用
org.springframework.util.xml.CachingMapDecorator
org.springframework.util.xml.ClassUtils用于Class的處理
org.springframework.util.xml.CollectionUtils用于處理集合的工具
org.springframework.util.xml.CommonsLogWriter
org.springframework.util.xml.CompositeIterator
org.springframework.util.xml.ConcurrencyThrottleSupport
org.springframework.util.xml.CustomizableThreadCreator
org.springframework.util.xml.DefaultPropertiesPersister
org.springframework.util.xml.DigestUtils摘要處理, 這里有用于md5處理信息的
org.springframework.util.xml.FileCopyUtils文件的拷貝處理, 結(jié)合Resource的概念一起來處理, 真的是很方便
org.springframework.util.xml.FileSystemUtils
org.springframework.util.xml.LinkedCaseInsensitiveMap
key值不區(qū)分大小寫的LinkedMap
org.springframework.util.xml.LinkedMultiValueMap一個(gè)key可以存放多個(gè)值的LinkedMap
org.springframework.util.xml.Log4jConfigurer一個(gè)log4j的啟動(dòng)加載指定配制文件的工具類
org.springframework.util.xml.NumberUtils處理數(shù)字的工具類, 有parseNumber 可以把字符串處理成我們指定的數(shù)字格式, 還支持format格式, convertNumberToTargetClass 可以實(shí)現(xiàn)Number類型的轉(zhuǎn)化.
org.springframework.util.xml.ObjectUtils有很多處理null object的方法. 如nullSafeHashCode, nullSafeEquals, isArray, containsElement, addObjectToArray, 等有用的方法
org.springframework.util.xml.PatternMatchUtilsspring里用于處理簡(jiǎn)單的匹配. 如 Spring's typical &quot;xxx*&quot;, &quot;*xxx&quot; and &quot;*xxx*&quot; pattern styles
org.springframework.util.xml.PropertyPlaceholderHelper用于處理占位符的替換
org.springframework.util.xml.ReflectionUtils反映常用工具方法. 有 findField, setField, getField, findMethod, invokeMethod等有用的方法
org.springframework.util.xml.SerializationUtils用于java的序列化與反序列化. serialize與deserialize方法
org.springframework.util.xml.StopWatch一個(gè)很好的用于記錄執(zhí)行時(shí)間的工具類, 且可以用于任務(wù)分階段的測(cè)試時(shí)間. 最后支持一個(gè)很好看的打印格式. 這個(gè)類應(yīng)該經(jīng)常用
org.springframework.util.xml.StringUtils
org.springframework.util.xml.SystemPropertyUtils
org.springframework.util.xml.TypeUtils用于類型相容的判斷. isAssignable
org.springframework.util.xml.WeakReferenceMonitor弱引用的監(jiān)控

和web相關(guān)的工具

org.springframework.web.util.CookieGenerator
org.springframework.web.util.HtmlCharacterEntityDecoder
org.springframework.web.util.HtmlCharacterEntityReferences
org.springframework.web.util.HtmlUtils
org.springframework.web.util.HttpUrlTemplate
這個(gè)類用于用字符串模板構(gòu)建url, 它會(huì)自動(dòng)處理url里的漢字及其它相關(guān)的編碼. 在讀取別人提供的url資源時(shí), 應(yīng)該經(jīng)常用
String url = &quot;http://localhost/myapp/{name}/{id}&quot;
org.springframework.web.util.JavaScriptUtils
org.springframework.web.util.Log4jConfigListener
用listener的方式來配制log4j在web環(huán)境下的初始化
org.springframework.web.util.UriTemplate
org.springframework.web.util.UriUtils處理uri里特殊字符的編碼
org.springframework.web.util.WebUtils
org.springframework.web.util.
4.EncodedResource(Resource對(duì)象,"UTF-8") 編碼資源(特殊的);
5.WebApplicationContextUtils
6.StringEscapeutils 編碼解碼

總結(jié)

以上就是本文關(guān)于簡(jiǎn)單了解Spring中常用工具類的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:Java語(yǔ)言Lang包下常用的工具類介紹淺談Springboot之于Spring的優(yōu)勢(shì)、Java之Spring注解配置bean實(shí)例代碼解析等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。

相關(guān)文章

  • java之swing單選框用法實(shí)例分析

    java之swing單選框用法實(shí)例分析

    這篇文章主要介紹了java之swing單選框用法,以實(shí)例形式分析了swing圖形界面單選框的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • Java遍歷Properties所有元素的方法實(shí)例

    Java遍歷Properties所有元素的方法實(shí)例

    這篇文章主要介紹了Java如何遍歷Properties所有元素的方法,大家可以參考使用
    2013-11-11
  • springboot實(shí)現(xiàn)后臺(tái)上傳圖片(工具類)

    springboot實(shí)現(xiàn)后臺(tái)上傳圖片(工具類)

    這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)后臺(tái)上傳圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 一文詳解Java中的原子操作

    一文詳解Java中的原子操作

    在Java中,原子操作尤為重要,尤其是在多線程環(huán)境中,想象一下,如果小黑在操作一個(gè)共享變量時(shí),這個(gè)操作被其他線程打斷,那會(huì)發(fā)生什么?可能會(huì)導(dǎo)致數(shù)據(jù)不一致,或者更糟糕的情況,本文將給大家詳細(xì)介紹一下Java中的原子操作
    2024-01-01
  • 解決SpringSecurity 一直登錄失敗的問題

    解決SpringSecurity 一直登錄失敗的問題

    這篇文章主要介紹了解決SpringSecurity 一直登錄失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲代碼示例

    java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲代碼示例

    這篇文章主要介紹了java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲代碼示例,還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-11-11
  • Spring Boot通過Junit實(shí)現(xiàn)單元測(cè)試過程解析

    Spring Boot通過Junit實(shí)現(xiàn)單元測(cè)試過程解析

    這篇文章主要介紹了Spring Boot通過Junit實(shí)現(xiàn)單元測(cè)試過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 詳解SpringBoot與SpringCloud的版本對(duì)應(yīng)詳細(xì)版

    詳解SpringBoot與SpringCloud的版本對(duì)應(yīng)詳細(xì)版

    這篇文章主要介紹了詳解SpringBoot與SpringCloud的版本對(duì)應(yīng)詳細(xì)版,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java多線程之synchronized關(guān)鍵字的使用

    Java多線程之synchronized關(guān)鍵字的使用

    這篇文章主要介紹了Java多線程之synchronized關(guān)鍵字的使用,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • spring boot與ktor整合的實(shí)現(xiàn)方法

    spring boot與ktor整合的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于spring boot與ktor整合的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評(píng)論