Spring的資源文件訪問功能使用起來十分簡單,調用ApplicationContext.getResource的方法即可:
- Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
- Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
- Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt");
- Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
Resource template = ctx.getResource("some/resource/path/myTemplate.txt"); Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt"); Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt"); Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
二)resource的深入瞭解
其實知道了以上的gerResource方法,就已經能夠滿足我們日常的資源文件訪問的需要了。但本着精益求精的精神,我們不妨看下spring的源碼,看看spring到底是如何實現這個功能模塊的。首先是接口Resource:
- public interface Resource extends InputStreamSource {
- boolean exists();
- boolean isOpen();
- URL getURL() throws IOException;
- File getFile() throws IOException;
- Resource createRelative(String relativePath) throws IOException;
- String getFilename();
- String getDescription();
- }
- public interface InputStreamSource {
- InputStream getInputStream() throws IOException;
- }
public interface Resource extends InputStreamSource { boolean exists(); boolean isOpen(); URL getURL() throws IOException; File getFile() throws IOException; Resource createRelative(String relativePath) throws IOException; String getFilename(); String getDescription(); } public interface InputStreamSource { InputStream getInputStream() throws IOException; }
基於這個底層接口,spring運用多態繼承了一系列的子類以滿足不同形式的資源文件訪問:
UrlResource: file: http: ftp: 的資源訪問形式都是它處理啦
ClassPathResource: 看名字也猜的出來classpath:的資源訪問形式就是它處理的
FileSystemResource: 從文件系統路徑獲取資源文件,C:\XXXXX
ServletContextResource: web應用中從context下獲取資源文件,相對路徑絕對路徑均可
InputStreamResource: 缺省的Resource類,如果根據輸入的url形式找不到對應的Resource,那麼就調用它了...當然我們也可以顯示的調用它:
- @Test
- public void testInputStreamResource() {
- ByteArrayInputStream bis = new ByteArrayInputStream("Hello World!".getBytes());
- Resource resource = new InputStreamResource(bis);
- if(resource.exists()) { ...... }
- }
@Test public void testInputStreamResource() { ByteArrayInputStream bis = new ByteArrayInputStream("Hello World!".getBytes()); Resource resource = new InputStreamResource(bis); if(resource.exists()) { ...... } }
ByteArrayResource: 看代碼便知...
- @Test
- public void testByteArrayResource() {
- Resource resource = new ByteArrayResource("Hello World!".getBytes());
- if(resource.exists()) { ...... }
- }
@Test public void testByteArrayResource() { Resource resource = new ByteArrayResource("Hello World!".getBytes()); if(resource.exists()) { ...... } }
三)ResourceLoaderAware
和其它Aware一樣,繼承此接口後,當此bean實例化時會自動調用setResourceLoader(ResourceLoader resourceLoader)方法。
- public interface ResourceLoaderAware {
- void setResourceLoader(ResourceLoader resourceLoader);
- }
public interface ResourceLoaderAware { void setResourceLoader(ResourceLoader resourceLoader); }
四) Resource的注入
那麼,如果是一個resouce類,Spring在配置文件中改如何處理以使它自動注入呢?樣例如下:
- <bean id="myBean" class="...">
- <property name="template" value="some/resource/path/myTemplate.txt"/>
- </bean>
- <property name="template" value="classpath:some/resource/path/myTemplate.txt">
- <property name="template" value="file:/some/resource/path/myTemplate.txt"/>
<bean id="myBean" class="..."> <property name="template" value="some/resource/path/myTemplate.txt"/> </bean> <property name="template" value="classpath:some/resource/path/myTemplate.txt"> <property name="template" value="file:/some/resource/path/myTemplate.txt"/>
十分方便吧~~
五)資源文件的通配樣式
當你需要一次獲取多個資源文件時,你可以考慮採用以下一些通配的手段:
- /WEB-INF/*-context.xml
- com/mycompany/**/applicationContext.xml
- file:C:/some/path/*-context.xml
- classpath:com/mycompany/**/applicationContext.xml
- classpath*:conf/appContext.xml //獲取classpath內所有的conf/appContext.xml,不加*的話只會加載搜索時遇到的第一個匹配文件