Load Resource using path wildcard,


http://blog.csdn.net/liufengyinglxj/article/details/8646417 (this one is better)

Here is another one:

Spring provides a powerful Ant pattern wildcard matching, which can match a batch of resources from the same path.

Ant path wildcard supports "?", "*", "**", note that wildcard matching does not include the directory separator "/".

"?": Match one character, such as "config?.xml" to match "config1.xml".

"*": Match zero or more strings, such as "com/*/config.xml" to match "cn /feng/config.xml", but does not match "com/config.xml" (because it matches a string here, which is OK if it's a directory); while "com/config-*.xml" will match "com/config.xml" /config-dao.xml";

"**": matches zero or more directories in the path. For example "com/**/config.xml" will match "com/config.xml" and also "com/feng/spring/config.xml"; and "com/feng/config-**.xml" will match "com/feng/config-dao.xml", that is, treat "**" as two "*".

When loading classpath resources, Spring provides a prefix "classpath:" to support loading a Resource, and a prefix "classpath*:" to support loading all matching classpath resources.

Spring provides the ResourcePatternResolver interface to load multiple Resource.

[java] view plain copy

    package com.feng.spring.chapter2.helloworld; 
     
    import java.io.IOException; 
     
    import org.springframework.core.io.Resource; 
    import org.springframework.core.io.ResourceLoader; 
     
    public interface ResourcePatternResolver extends ResourceLoader{ 
        String CLASSPATH_ALL_URL_PREFIX = " classpath*:"; 
        Resource[] getResources(String locationPattern) throws IOException;//This method is added to receive multiple Resources 
    } 


1. "classpath": used to load one of the class paths (including jar packages) and only A resource; only one is returned for multiple matches. Consider the "classpath*." prefix if multiple matches are required.

[java] view plain copy

    package com.feng.spring.chapter2.helloworld; 
     
     
    import java.io.IOException; 
     
    import org.junit.Test; 
    import org.springframework.core.io.Resource; 
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 
     
     
    public class HelloTest { 
            @Test 
            public void testClasspathPrefix() throws IOException{ 
                ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); 
                / /Only load one absolutely matching Resource, and load it through ResourceLoader.getResource 
                Resource resources=resolver.getResource("classpath:META-INF/INDEX.LIST"); 
                 Assert.assertEquals(1, resources.length); 
                //Only load A matching Resource, loaded via ResourceLoader.getResource 
                 resources = resolver.getResource("classpath:META-INF/*.LIST"); 
                 Assert.assertTrue(resources.length == 1);  
            } 
    } 


Second, "classpath*":

used to load the classpath (including jar packages) All matching resources in .

[java] view plain copy

    package com.feng.spring.chapter2.helloworld; 
     
     
    import java.io.IOException; 
     
    import javax.annotation.Resource; 
     
    import junit.framework.Assert; 
     
    import org.junit.Test; 
    import org.springframework. core.io.support.PathMatchingResourcePatternResolver; 
     
     
    public class HelloTest { 
            @Test 
            public void testClasspathAsteriskPrefix() throws IOException{ 
                ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); 
                 
                //All Resources with multiple absolute matches will be loaded 
                //The non-pattern path part will be loaded first through ClassLoader.getResource("META-INF") 
                //Then traverse pattern matching 
                Resource [] resources = (Resource[]) resolver.getResources("classpath*:META-INF/INDEX.LIST");  
                Assert.assertTrue(resources.length > 1);   
                //Resource resources that match multiple patterns will be loaded 
                = (Resource[]) resolver.getResources("classpath*:META-INF/*.LIST"); 
                 Assert.assertTrue(resources.length > 1);    
            } 
    } 




The classpath with wildcards uses the "Enumeration<URL> getResources(String name)" method of "ClassLoader" to find the resources before the wildcards, and then obtains the matching resources through pattern matching. For example, "classpath:META-INF/*.LIST" will first load the directory "META-INF" before the wildcard, and then traverse the path to perform sub-path matching to obtain matching resources.

3. "file": Load Resource in one or more systems. Such as: "file:D/*.txt" will return all the txt files under the D drive.

4. No prefix: Load a resource through ResourceLoader.

The getResource method provided by ApplicationContext delegates the acquisition of resources to the ResourcePatternResolver implementation, which uses PathMatingResourcePatternResolver by default.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326845386&siteId=291194637
Recommended