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初學快速入門
