Spring资源加载器提供了一个非常通用的getResource()方法来从文件系统,类路径或URL(文本文件,媒体文件,图像文件...)得到资源。你可以从应用程序上下文中得到 getResource()方法。
下面是一个例子来说明如何使用 getResource()加载文本文件从
1. 文件系统
Resource resource = appContext.getResource("file:c:\\testing.txt");
2. URL路径
Resource resource =
appContext.getResource("url:http://www.yourdomain.com/testing.txt");
3. 类路径
Resource resource =
appContext.getResource("classpath:com/zaixian/common/testing.txt");
只需要指定资源的位置,Spring将处理其余部分,并返回一个资源对象。
例如,完全用的getResource()方法。
package com.xuhuhu.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class App
{
public static void main( String[] args )
{
ApplicationContext appContext =
new ClassPathXmlApplicationContext(new String[] {"If-you-have-any.xml"});
Resource resource =
appContext.getResource("classpath:com/zaixian/common/testing.txt");
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
bean资源加载器(ResourceLoaderAware)
因为 bean 没有应用程序上下文的访问,一个bean如何访问资源?解决方法是实现 ResourceLoaderAware 接口,并为资源加载对象 setter 方法. Spring将尽的资源加载到bean。
package com.zaixian.customer.services;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class CustomerService implements ResourceLoaderAware
{
private ResourceLoader resourceLoader;
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public Resource getResource(String location){
return resourceLoader.getResource(location);
}
}
Bean配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService"
class="com.zaixian.customer.services.CustomerService" />
</beans>
执行它
package com.xuhuhu.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import com.zaixian.customer.services.CustomerService;
public class App
{
public static void main( String[] args )
{
ApplicationContext appContext =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust =
(CustomerService)appContext.getBean("customerService");
Resource resource =
cust.getResource("classpath:com/zaixian/common/testing.txt");
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
现在,可以从一个 bean 获得资源。
总结
如果没有这个 getResource()方法,将需要处理不同的解决方案,如文件对象的文件系统资源,对URL资源的URL对象不同的资源。Spring真的很好地与这个超级通用的getResource()方法工作,它确实可以帮我们节省处理资源的时间。
上一篇:
Spring依赖注入servlet会话监听器
下一篇:
Spring初学快速入门
