- 目标
- 了解资源表示形式
- 了解资源访问、
- SPring的资源管理在JDK的基础功能上进行了强大的拓展
- 隐藏底层实现
- 新增资源存在判断、资源操作权限相关的功能,相对于java.net.URL资源不存在则设置好为null更友好
- 支持通配符获取资源
- Spring管理资源(如下):
- UrlResource
- ClassPathResource
- FileSystemResource
- ServletContextResource
- InputStreamResource
- ByreArrayResource
- 资源协议与路径
- Spring使用Resource接口,访问底层资源:
public interface Resource extends InputStreamSource{
boolean exists();
boolean isOpen();
URL getURL() throows IOException;
File getFile() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDEscription();
}
public interface InputStreamSource(){
InputStream getInputStream() throws IOEception;
}
- 使用ResourceLoader接口记载资源
- 应用上下文与资源
- 从classpath中加载资源
-
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml"); ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- 从当前程序运行的工作目录,用相对路径加载资源
- 使用指定前缀,从classpath中加载资源
- 使用通配符 * 加载资源
-
Mybatis配置映射文件时使用的资源串与Spring相似: