Spring 學習記錄4 ResourceLoader

ResourceLoader

Spring的ApplicationContext繼承了ResourceLoader接口.這個接口主要就是能夠加載各類resource..html

接口仍是比較簡單的:web

 1 /*
 2  * Copyright 2002-2014 the original author or authors.
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.core.io;
18 
19 import org.springframework.util.ResourceUtils;
20 
21 /**
22  * Strategy interface for loading resources (e.. class path or file system
23  * resources). An {@link org.springframework.context.ApplicationContext}
24  * is required to provide this functionality, plus extended
25  * {@link org.springframework.core.io.support.ResourcePatternResolver} support.
26  *
27  * <p>{@link DefaultResourceLoader} is a standalone implementation that is
28  * usable outside an ApplicationContext, also used by {@link ResourceEditor}.
29  *
30  * <p>Bean properties of type Resource and Resource array can be populated
31  * from Strings when running in an ApplicationContext, using the particular
32  * context's resource loading strategy.
33  *
34  * @author Juergen Hoeller
35  * @since 10.03.2004
36  * @see Resource
37  * @see org.springframework.core.io.support.ResourcePatternResolver
38  * @see org.springframework.context.ApplicationContext
39  * @see org.springframework.context.ResourceLoaderAware
40  */
41 public interface ResourceLoader {
42 
43     /** Pseudo URL prefix for loading from the class path: "classpath:" */
44     String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
45 
46 
47     /**
48      * Return a Resource handle for the specified resource.
49      * The handle should always be a reusable resource descriptor,
50      * allowing for multiple {@link Resource#getInputStream()} calls.
51      * <p><ul>
52      * <li>Must support fully qualified URLs, e.g. "file:C:/test.dat".
53      * <li>Must support classpath pseudo-URLs, e.g. "classpath:test.dat".
54      * <li>Should support relative file paths, e.g. "WEB-INF/test.dat".
55      * (This will be implementation-specific, typically provided by an
56      * ApplicationContext implementation.)
57      * </ul>
58      * <p>Note that a Resource handle does not imply an existing resource;
59      * you need to invoke {@link Resource#exists} to check for existence.
60      * @param location the resource location
61      * @return a corresponding Resource handle
62      * @see #CLASSPATH_URL_PREFIX
63      * @see org.springframework.core.io.Resource#exists
64      * @see org.springframework.core.io.Resource#getInputStream
65      */
66     Resource getResource(String location);
67 
68     /**
69      * Expose the ClassLoader used by this ResourceLoader.
70      * <p>Clients which need to access the ClassLoader directly can do so
71      * in a uniform manner with the ResourceLoader, rather than relying
72      * on the thread context ClassLoader.
73      * @return the ClassLoader (only {@code null} if even the system
74      * ClassLoader isn't accessible)
75      * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
76      */
77     ClassLoader getClassLoader();
78 
79 }
View Code

我感受主要可能就是getResource方法了.spring

 

具體使用

實驗以下:express

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @ContextConfiguration("classpath:test-application-context.xml")
 3 public class ResourceLoaderTest implements ApplicationContextAware {
 4     ApplicationContext applicationContext;
 5 
 6     @Test
 7     public void testLoadResource() throws IOException {
 8 
 9         Resource resource = applicationContext.getResource("classpath:test.properties");
10         System.out.println(resource.exists()); // true
11 
12         Resource resource2 = applicationContext.getResource("/test.properties");
13         System.out.println(resource2.exists()); // true
14 
15         Resource resource3 = applicationContext.getResource("test.properties");
16         System.out.println(resource3.exists()); // true
17 
18         Resource resource4 = applicationContext.getResource("classpath:1.html");
19         System.out.println(FileUtils.readFileToString(resource4.getFile())); // 文件內容
20 
21         Resource resource5 = applicationContext.getResource("https://www.baidu.com/");
22         System.out.println(IOUtils.toString(resource5.getInputStream())); // 網頁內容
23 
24         Resource resource6 = applicationContext.getResource("/spring/Config.class");
25         System.out.println(resource6.exists()); // true
26 
27         Resource resource7 = applicationContext.getResource("org/springframework/context/support/GenericApplicationContext.class");
28         System.out.println(resource7.exists()); // true
29     }
30 
31     @Test
32     public void a() {
33         System.out.println(1);
34     }
35 
36     @Override
37     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
38         this.applicationContext = applicationContext;
39     }
40 }

實驗中發現:apache

1.能夠經過classpath:XXX下載classpath下的資源windows

2.能夠經過/XXX也是加載classpath下的資源app

3.直接XXX,能夠根據不一樣的協議去加載資源(好比http),沒有的話去加載classpath下的資源less

4.不光能夠加載classes下的資源,也能夠加載lib裏jar裏面的資源.ide

 

我用的junit 測試,applicationcontext是GenericApplicationContext的實例,getResource方法調的是DefaultResourceLoader的實現學習

 1     @Override
 2     public Resource getResource(String location) {
 3         Assert.notNull(location, "Location must not be null");
 4         if (location.startsWith("/")) {
 5             return getResourceByPath(location);
 6         }
 7         else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
 8             return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
 9         }
10         else {
11             try {
12                 // Try to parse the location as a URL...
13                 URL url = new URL(location);
14                 return new UrlResource(url);
15             }
16             catch (MalformedURLException ex) {
17                 // No URL -> resolve as resource path.
18                 return getResourceByPath(location);
19             }
20         }
21     }

從代碼中咱們能夠發現,

1.若是是/開頭的資源,會調用getResourceByPath方法,最後返回的其實也是ClassPathResource

 1     /**
 2      * Return a Resource handle for the resource at the given path.
 3      * <p>The default implementation supports class path locations. This should
 4      * be appropriate for standalone implementations but can be overridden,
 5      * e.g. for implementations targeted at a Servlet container.
 6      * @param path the path to the resource
 7      * @return the corresponding Resource handle
 8      * @see ClassPathResource
 9      * @see org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
10      * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
11      */
12     protected Resource getResourceByPath(String path) {
13         return new ClassPathContextResource(path, getClassLoader());
14     }
15 
16 
17     /**
18      * ClassPathResource that explicitly expresses a context-relative path
19      * through implementing the ContextResource interface.
20      */
21     protected static class ClassPathContextResource extends ClassPathResource implements ContextResource {
22 
23         public ClassPathContextResource(String path, ClassLoader classLoader) {
24             super(path, classLoader);
25         }
26 
27         @Override
28         public String getPathWithinContext() {
29             return getPath();
30         }
31 
32         @Override
33         public Resource createRelative(String relativePath) {
34             String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
35             return new ClassPathContextResource(pathToUse, getClassLoader());
36         }
37     }
View Code

2.若是是classpath:開頭,也是ClassPathResource

3.若是是XXX.XX的話用URL去找資源失敗的話,仍是會返回ClassPathResource,成功的話就是返回UrlResource.

 

而後我想到了1個問題.就是咱們在項目中加載文件的時候常常會用classpath*:.............這種形式在這裏彷佛沒有出現,多是Servlet環境下的ApplicationContext覆蓋了getResource方法,也多是其餘方法加載資源..等我學習了其餘的applicationcontext就明白了...可能會再作分享.

 

 

小結

我感受使用resourceloader相比於getResource裏面寫classpath:xxxxxx比本身去getClass().getResource的好處在於:

1.更簡單清晰...看過去就知道資源是相對於classpath的...

2.resourceloader產生的ClassPathResource對於你傳入的路徑字符串是會轉化的...你傳入的windows的\也會被轉化成/..而getClass那種並不會....因此getClass().getResource在傳入的String拼接的時候若是用到了File.sperator可能會找不到資源,而resourceloader不會...不過更多的時候可能都不須要拼接...直接寫1個完整的字符串用/分割路徑就好了...

相關文章
相關標籤/搜索