spring判空工具类_Spring常用一些工具类实例汇总

本文详细介绍了Spring框架中的一些实用工具类,包括Resource类型的UrlResource、ClassPathResource等,用于处理文件资源;本地化文件资源的LocalizedResourceHelper;操作Servlet API的WebApplicationContextUtils;XML工具类如DomUtils和StaxUtils;web相关的HtmlUtils和UriTemplate;参数检测工具类Assert;请求处理工具类ServletRequestUtils;以及其他如StringUtils、SerializationUtils等。这些工具类极大地简化了开发工作,提高效率。
摘要由CSDN通过智能技术生成

一、内置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常用一些工具类实例汇总

本文地址: http://www.cppcns.com/ruanjian/java/367904.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值