`
friendsys
  • 浏览: 338429 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

spring 下的一些Utils

阅读更多
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下也提供了对请求的参数获取的处理方式



























分享到:
评论

相关推荐

    HttpClientUtils.java

    httpclient工具类包含get,post,put,delete请求

    base64utils jar 包下载

    base64utils jar 项目中应用,亲测,希望能帮到您,加油 少年

    spring-web-2.5.jar

    org.springframework.web.util.JavaScriptUtils.class org.springframework.web.util.Log4jConfigListener.class org.springframework.web.util.Log4jConfigServlet.class org.springframework.web.util.Log4...

    springboot 集成封装redis工具类redisUtils和单元测试

    文章是redisUtils工具类的封装,和基于springboot环境的单元测试; 文中封装了redis的众多方法,希望对您有所帮助。

    spring-boot-utils:Spring Boot 1和Spring Boot 2兼容的utils

    Spring Boot 1和Spring Boot 2兼容的utils。 在cnetral信息库中最新版本: < groupId>io.github.hengyunabc < artifactId>spring-boot-utils $version 条件注释 @ ConditionalOnSpringBoot1 @ ...

    spring-hibernate-query-utils:库提供工具来检测N + 1查询并计算使用Spring和Hibernate生成的查询

    Spring Hibernate查询实用程序Spring应用程序中不再有N + 1个查询Spring Hibernate Query Utils:一种在Spring / Hibernate应用程序中检测N + 1查询并计算查询次数的简便方法·目录用夹具测试检测配置使能够错误等级...

    RedisUtils.java

    基于spring和redis的redisTemplate工具类,针对redisTemplate的一些封装,包含一些日常比较容易用到的一些通用方法。

    可能是方法最全Redis静态工具类(RedisUtils.java)

    可能是最全的Redis静态工具类,拿来即用,随时随地RedisUtils.方法名()存取数据,每个方法都有对应的注释,快速上手!

    springcloud技术点

    RoketMQ、Kafka、MongoDB、OSS、Redis、Swagger、Zuul、Label、BASE、Charts、Utils DAO: Spring Data、Mybatis、OSS、 DTO Data Storage: RDBS DFS、NOSQL/Hadoop Infrastructure: LogBack、BUS、Jenkins、...

    Spring Utils工具类常用方法实例

    主要介绍了Spring Utils工具类常用方法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    techaction-common-filter-utils.zip

    Spring+SpringMVC+DBUtils系统框架下的common包、filter包和utils包中的常用类,详情请见https://blog.csdn.net/Joyce_Ff/article/details/93377981

    spring-utils:实用程序集合,以支持ICTS开发的Web应用程序。 旨在与Spring和Hibernate一起开发

    spring-utils 实用程序集合,以支持ICTS开发的Web应用程序。 旨在与Spring和Hibernate一起开发。 执照 该项目是在2.0版下。

    excelUtils.zip

    这是导出excel的一个工具类,仅供学习使用,如真实开发环境使用此工具类出现问题,这里不负责!可以通过这个工具类学习简单的poi导出excel操作,也可以比着自己实现一个自己的工具类,毕竟用自己的才安全放心。

    Spring-Boot-Reference.pdf

    文档介绍了一些但不限于以下内容的相关技术细节: •构建系统:Maven | Gradle |和|初学者 •最佳实践:代码结构| @Configuration | @EnableAutoConfiguration | Beans和依赖注入 •运行代码: IDE |打包| Maven | ...

    Spring所有jar包

    里面提供了Spring的AOP,DAO,DATA,IOC,JSON,MVC,ORM,TASK,TEST包以及applicationContext.xml配置文件,适合Spring框架初学者及老鸟下载使用

    spring cloud api gateway简介

    讲得很不错的一段,驰狼的 spring cloud api gateway 简介

    maven+springmvc+spring+hibernate+freemarker

    maven管理项目+springmvc+spring+hibernate+freemarker 集成框架...参考utils下的DateUtils.java类。 slf4j日志文件有详细的配置。另外还有几个小例子可以试运行下。有什么问题都可以沟通讨论。 欢迎大家讨论更好的方案

    springboot-util:Spring Boot 项目工具类,框架集成记录

    areadisplay 中包含了小程序所需要的代码,t_area.sql 是数据库导出的结构文件,src 目录下为主要的后端代码。 包括了一些配置信息与基本的增删改查代码,刚入门的同学可以参考这个 demo。 springboot-chart 柱状图...

    spring-xmemcached

    spring+xmemcached aop切面 需要xmemcached-1.2.5+spring-2.5.6 class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown"> ${XMemcached_servers} <!-- ...

    springinaction2-P2c5.pic

    springinaction2-P2c5.pic.rar springinaction2-P2c5.pic.rar

Global site tag (gtag.js) - Google Analytics