spring 下的一些Utils

本文介绍了Spring框架中多种实用工具类,如HtmlUtils、JavaScriptUtils等,用于HTML和JavaScript特殊字符转义;Assert用于内容判断;Resource接口及其实现类提供资源访问支持;FileCopyUtils简化文件操作;PropertiesLoaderUtils简化属性文件加载;WebApplicationContextUtils帮助获取WebApplicationContext对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

07年的文章,对Spring提供的工具类提供了介绍,可以安排时间看下相应的源码

Spring 为 HTML 和 JavaScript 特殊字符提供了转义操作工具类,它们分别是 HtmlUtils 和 JavaScriptUtils。
org.springframework.web.util.HtmlUtils 提供对HTML字符串中的符号进行过滤
JavaScriptUtils 对Js提供过滤
spring/lib/jakarta-commons/commons-lang.jar)的 StringEscapeUtils提供了更高级的功能,包括了对sql的过滤,防止被注入 , 似乎就是提供了字符串的""与''的转义

org.springframework.util.Assert; 下面提供一些对内容判断的方法,类似xUnit类,如果验证不通过,将直接抛出异常,主要允许定制异常信息

org.springframework.core.io.Resource 接口 ,为访问资源提供了统一的接口
Resource res2 = new ClassPathResource("conf/file1.txt");
Resource res1 = new FileSystemResource("d:/filePath");

在界面中,也可以通过
Resource res3 = new ServletContextResource(application, "/WEB-INF/classes/conf/file1.txt");

ResourceUtils 工具类,支持带classpath: file:的路径访问模式
File clsFile = ResourceUtils.getFile("classpath:conf/file1.txt");
String httpFilePath = "file:D:/masterSpring/chapter23/src/conf/file1.txt";
File httpFile = ResourceUtils.getFile(httpFilePath);

LocalizedResourceHelper 也可以用于提供对不同区域的资源文件自动加载
LocalizedResourceHelper lrHalper = new LocalizedResourceHelper();
// ① 获取对应美国的本地化文件资源
Resource msg_us = lrHalper.findLocalizedResource("i18n/message", ".properties",
Locale.US);
// ② 获取对应中国大陆的本地化文件资源
Resource msg_cn = lrHalper.findLocalizedResource("i18n/message", ".properties",
Locale.CHINA);
System.out.println("fileName(us):"+msg_us.getFilename());
System.out.println("fileName(cn):"+msg_cn.getFilename());

相对 java.util.ResourceBundle提供的获取资源文件的方式,spring提供了更加面向接口的工具类

FileCopyUtils 提供了许多一步式的静态操作方法,能够将文件内容拷贝到一个目标 byte[]、String 甚至一个输出流或输出文件中
byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile());
String fileStr = FileCopyUtils.copyToString(new FileReader(res.getFile()));
FileCopyUtils.copy(res.getFile(), new File(res.getFile().getParent()+ "/file2.txt"));
主要便利就是提供了异常和io开关的处理
OutputStream os = new ByteArrayOutputStream();
FileCopyUtils.copy(res.getInputStream(), os);

PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源
Properties props = PropertiesLoaderUtils.loadAllProperties("jdbc.properties") //节约了代码的调用
此外,PropertiesLoaderUtils 还可以直接从 Resource 对象中加载属性资源

这里注意编码的问题,需要对resource进行编码处理
Resource res = new ClassPathResource("conf/file1.txt");
// ① 指定文件资源对应的编码格式(UTF-8)
EncodedResource encRes = new EncodedResource(res,"UTF-8");

Spring 容器在启动时将 WebApplicationContext 保存在 ServletContext的属性列表中,通过 WebApplicationContextUtils 工具类可以方便地获取 WebApplicationContext 对象
WebApplicationContext wac = (WebApplicationContext)servletContext.
getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

但通过位于 org.springframework.web.context.support 包中的 WebApplicationContextUtils 工具类获取 WebApplicationContext 更方便:
WebApplicationContext wac =WebApplicationContextUtils.
getWebApplicationContext(servletContext);

WebUtils 提供了大量servlet api的调用,缩短了原有调用的代码量

IntrospectorCleanupListener 监听器 用于处理使用了 JavaBean Introspector 分析应用中的类,ntrospector 缓存会保留这些类的引用,从而导致的GC异常

ServletRequestUtils下也提供了对请求的参数获取的处理方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值