电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring常用一些工具類實例匯總

瀏覽:101日期:2023-07-29 11:27:22

一、內置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 弱引用的監控

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 武汉高低温试验机-现货恒温恒湿试验箱-高低温湿热交变箱价格-湖北高天试验设备 | 定制/定做衬衫厂家/公司-衬衫订做/订制价格/费用-北京圣达信 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 不锈钢散热器,冷却翅片管散热器厂家-无锡市烨晟化工装备科技有限公司 | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | 杭州翻译公司_驾照翻译_专业人工翻译-杭州以琳翻译有限公司官网 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 | 防水接头-电缆防水接头-金属-电缆密封接头-不锈钢电缆接头 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 定制/定做冲锋衣厂家/公司-订做/订制冲锋衣价格/费用-北京圣达信 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 | 最新范文网_实用的精品范文美文网| 代理记账_免费注册公司_营业执照代办_资质代办-【乐财汇】 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 消防泵-XBD单级卧式/立式消防泵-上海塑泉泵阀(集团)有限公司 | wika威卡压力表-wika压力变送器-德国wika代理-威卡总代-北京博朗宁科技 | 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | 消泡剂-水处理消泡剂-涂料消泡剂-切削液消泡剂价格-东莞德丰消泡剂厂家 | 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 预制舱-电力集装箱预制舱-模块化预制舱生产厂家-腾达电器设备 | 闪电优家-卫生间防水补漏_酒店漏水渗水维修_防水堵漏公司 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 浙江工业冷却塔-菱电冷却塔厂家 - 浙江菱电冷却设备有限公司 | 气动隔膜泵-电动隔膜泵-循环热水泵-液下排污/螺杆/管道/化工泵「厂家」浙江绿邦 | 灌木树苗-绿化苗木-常绿乔木-价格/批发/基地 - 四川成都途美园林 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 苏州西装定制-西服定制厂家-职业装定制厂家-尺品服饰西装定做公司 | 保温杯,儿童婴童奶瓶,运动水壶「广告礼品杯定制厂家」超朗保温杯壶 | 智慧水务|智慧供排水利信息化|水厂软硬件系统-上海敢创 | 冷热冲击试验箱_温度冲击试验箱价格_冷热冲击箱排名_林频厂家 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 玖容气动液压设备有限公司-气液增压缸_压力机_增压机_铆接机_增压器 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 10吨无线拉力计-2吨拉力计价格-上海佳宜电子科技有限公司 | 冷柜风机-冰柜电机-罩极电机-外转子风机-EC直流电机厂家-杭州金久电器有限公司 |