Resource


g
;;
Resources:针对资源文件的统一接口,通过Spring加载一些资源文件的时候,可以通过它去控制。
——UrlResource:URL对应的资源,根据一个URL地址即可构建Resources。
——ClassPathResoure:获取类路径下的资源文件(相对路径)。
——FileSystemResource:获取文件系统里面的资源(绝对路径)。
——ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源。(和Web相关的资源文件的入口)
——InputStreamResource:针对于输入流封装的资源。(构建它需要InputStream)
——ByteArrayResource:针对于字节数组封装的资源。(构建它需要ByteArray)
ResourceLoader:对Resource加载的一个类,在SpringIOC容器中,所有的ApplicationContext都实现了ResourceLoader接口,所有的ApplicationContext都可以用来获取Resource实例,所以可以通过getResource(String location)方法获取资源Resource。
ResourceLoader接口的声明(有个方法,输入为文件的位置,返回的是Resource的实例)

ResourceLoader注入参数时前缀的几种类型

ResourceLoader前缀:classpath:(相对路径,加载文件)
file:(绝对路径,加载文件)
url: http(web路径、加载文件)
(none):直接输入路径,依赖ApplicationContext
案例:(Bean通过实现ApplicationContext接口,间接的实现了ResourceLoader接口(加载Resource实例),就可以使用getResource()获取Resource的实例,Resource拥有一系列的方法,比如获取文件名称(getFilename()和获取文件长度contentLength())

步骤1:
public class ResourceDemo implements ApplicationContextAware {
private ApplicationContext ac;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
this.ac=ac;
}
public void resource() throws IOException{
Resource r=ac.getResource("classpath:resourceDemo.txt");(直接写文件,而不写全路径,是因为Java build path 配置了source,所以这里是相对路径)
System.out.println(r.getFilename());
System.out.println(r.contentLength());
}
}
步骤2:
<bean id="resourceDemo" class="ResourceDemo" ></bean>
步骤3:
@Test
public void testBean() throws IOException{
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");
rd.resource();
}
测试:
@Test
public void testBean() throws IOException{
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");
rd.resource();
}
结果:(文件:resourceDemo.txt,在src——>resource文件夹下)

resourceDemo.txt
6
案例:file方式

案例:url方式

ResourceLoader API
ResourceLoader
Resources
11111

当spring需要加载文件的时候会用到resources
resources 的加载类:

这里的resource是
org.springframework.core.io.Resource
里面的,不要导错了。
ResourceLoader
Resources
Resources


ResourceLoader 注入方式
resourceloader例子
resources
ResourceLoader
Resources
Resources:针对资源文件的统一接口,通过Spring加载一些资源文件的时候,可以通过它去控制。
——UrlResource:URL对应的资源,根据一个URL地址即可构建Resources。
——ClassPathResoure:获取类路径下的资源文件(相对路径)。
——FileSystemResource:获取文件系统里面的资源(绝对路径)。
——ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源。(和Web相关的资源文件的入口)
——InputStreamResource:针对于输入流封装的资源。(构建它需要InputStream)
——ByteArrayResource:针对于字节数组封装的资源。(构建它需要ByteArray)
ResourceLoader:对Resource加载的一个类,在SpringIOC容器中,所有的ApplicationContext都实现了ResourceLoader接口,所有的ApplicationContext都可以用来获取Resource实例,所以可以通过getResource(String location)方法获取资源Resource。
ResourceLoader接口的声明(有个方法,输入为文件的位置,返回的是Resource的实例)

ResourceLoader注入参数时前缀的几种类型

ResourceLoader前缀:classpath:(相对路径,加载文件)
file:(绝对路径,加载文件)
url: http(web路径、加载文件)
(none):直接输入路径,依赖ApplicationContext
ResourceLoader 注入方式:
Resources:针对于资源文件的统一接口
所有的 applicationContext 都实现了 resource 接口
如果什么都不写,就和 applicationContext 创建方式一样,例如 classpath: test.txt
Resources:针对资源文件的统一接口,通过Spring加载一些资源文件的时候,可以通过它去控制。
——UrlResource:URL对应的资源,根据一个URL地址即可构建Resources。
——ClassPathResoure:获取类路径下的资源文件(相对路径)。
——FileSystemResource:获取文件系统里面的资源(绝对路径)。
——ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源。(和Web相关的资源文件的入口)
——InputStreamResource:针对于输入流封装的资源。(构建它需要InputStream)
——ByteArrayResource:针对于字节数组封装的资源。(构建它需要ByteArray)
ResourceLoader:对Resource加载的一个类,在SpringIOC容器中,所有的ApplicationContext都实现了ResourceLoader接口,所有的ApplicationContext都可以用来获取Resource实例,所以可以通过getResource(String location)方法获取资源Resource。
ResourceLoader接口的声明(有个方法,输入为文件的位置,返回的是Resource的实例)

ResourceLoader注入参数时前缀的几种类型

ResourceLoader前缀:classpath:(相对路径,加载文件)
file:(绝对路径,加载文件)
url: http(web路径、加载文件)
(none):直接输入路径,依赖ApplicationContext
案例:(Bean通过实现ApplicationContext接口,间接的实现了ResourceLoader接口(加载Resource实例),就可以使用getResource()获取Resource的实例,Resource拥有一系列的方法,比如获取文件名称(getFilename()和获取文件长度contentLength())

步骤1:
public class ResourceDemo implements ApplicationContextAware {
private ApplicationContext ac;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
this.ac=ac;
}
public void resource() throws IOException{
Resource r=ac.getResource("classpath:resourceDemo.txt");(直接写文件,而不写全路径,是因为Java build path 配置了source,所以这里是相对路径)
System.out.println(r.getFilename());
System.out.println(r.contentLength());
}
}
步骤2:
<bean id="resourceDemo" class="ResourceDemo" ></bean>
步骤3:
@Test
public void testBean() throws IOException{
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");
rd.resource();
}
测试:
@Test
public void testBean() throws IOException{
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");
rd.resource();
}
结果:(文件:resourceDemo.txt,在src——>resource文件夹下)

resourceDemo.txt
6
案例:file方式

案例:url方式

ResourceLoader的bean装配
Resources 针对于资源文件的统一接口
Bean容器:
1)Bean配置项
2)Bean的作用域
3)Bean的生命周期
4)Bean的自动装配
5)Resources&ResourceLoader
针对于资源文件的统一接口:
Resources