以下为学习笔记
UrlResource:URL对应的资源,根据一个URL地址去构建
ClassPathResource:对应类路径下的资源文件(相对路径)
FileSystemResource:获取文件系统里面的资源(绝对路径)
ServletContextResource:ServletContext封装的资源用于访问ServletContext环境下的资源(与web相关的入口)
InputStreamResource:针对于输入流封装的资源
ByteArrayResource:针对于字节数组封装的资源
ResourceLoader
对resource进行加载
public interface ResourceLoader{
Resource gerResource(String location);
}
Resource remplate = ctx.getResource("some/resource/path/my.txt");
Resource remplate = ctx.getResource("classpath:some/resource/path/my.txt");
Resource remplate = ctx.getResource("file:/some/resource/path/my.txt");
http: --http://myserver/logo.png
(none) -- /data/config.xml
---------------------------------
public class MyResource implements ApplicationContextAware {
private ApplicationContext applicationContext;
//获取到applicationContext并且赋值
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
this.applicationContext = applicationContext;
}
public void resource(){
applicationContext.gerResource("classpath:config.txt");
applicationContext.gerResource("file:D:\\ddd\web_projects\\spring\\src\\main\\resources\\config.txt");
applicationContext.gerResource("url:http://docs.spring.io/spring/docs/4.0.5/RELEASE/spring-framework-reference/htmlsingle");
applicationContext.gerResource("config.txt");
//没有前缀的时候,依赖于applicationContext,默认classpath的方式加载,是取super("classpath:xxx")传入的那个classpath
}