Spring工具类

文件资源访问

1、统一资源访问接口

Resource

2、实现类

FileSystemResource 通过文件系统路径访问

ClassPathResource 通过classpath路径访问

ServletContextResource 相对于web根目录路径访问

3、工具类

ResourceUtils 通过classpath:和file:资源前缀路径访问

文件资源操作

1、FileCopyUtils

取代底层的文件操作

2、PropertiesLoaderUtils

操作properties文件(文件要存放在classpath下,否则需要传入resource对象)

3、EncodedResource

对Resource进行编码处理

Web工具类

1、WebApplicationContextUtils

获取WebApplicatoinContext对象

2、WebUtils

封装了原始的Servlet API方法

过滤器和监听器

1、延迟加载过滤器

 <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> 

2、中文乱码过滤器

<filter>
	<filter-name>CharacterEncodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>utf-8</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
 

3、Web 应用根目录监听器

可通过System.getProperty("mu.root")或在properties中${mu.root}获取web应用程序根目录

 <context-param> 
    <param-name>webAppRootKey</param-name> 
    <param-value>mu.root</param-value>
 </context-param> 
 <listener> 
    <listener-class> 
    org.springframework.web.util.WebAppRootListener 
    </listener-class> 
 </listener> 
 

4、Log4J 监听器

该监听器包含了WebAppRootListener的功能,所以配置了这个WebAppRootListener就不用配置了

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>
    org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>

注意:WebAppRootListener 和 Log4jConfigListener 都只能在Web应用部署后WAR 文件会解包的Web应用服务器上使用。

5、Introspector 缓存清除监听器

保证在 Web 应用关闭的时候释放与其相关的 ClassLoader 的缓存和类引用

<listener> 
    <listener-class> 
    org.springframework.web.util.IntrospectorCleanupListener 
    </listener-class> 
</listener>

HtmlUtils

对html字符串进行编码和解码处理

htmlEscape(String input)

htmlUnescape(String input)

Assert

对变更进行条件判断,如果不符合将抛出异常

例如:

Assert.hasText(str,”不能为空字符串”)

str不为null 且必须至少包含一个非空格的字符,否则抛出异常 java.lang.IllegalArgumentException: 不能为空字符串

原文地址:https://www.cnblogs.com/mu-mu/p/3831949.html