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/yiibai/common/testing.txt");
package com.tw511.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/yiibai/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 沒有應用程式上下文的存取,一個bean如何存取資源?解決方法是實現 ResourceLoaderAware 介面,並為資源載入物件 setter 方法. Spring將盡的資源載入到bean。
package com.yiibai.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.yiibai.customer.services.CustomerService" /> </beans>
執行它
package com.tw511.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.yiibai.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/yiibai/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(); } } }
如果沒有這個 getResource()方法,將需要處理不同的解決方案,如檔案物件的檔案系統資源,對URL資源的URL物件不同的資源。Spring真的很好地與這個超級通用的getResource()方法工作,它確實可以幫我們節省處理資源的時間。