4.4.1 Using Path Fugazai resource
The resource path described earlier is a very simple path to match a resource, and spring also provides a more powerful ant pattern wildcard match, from a path that can match a batch of resources.
Ant path wildcard support "? "," * "," * * ", note that wildcard matches do not include the directory delimiter"/":
"? ": Match a character , such as" Config?. XML "will match" Config1.xml ";
"* ": matches 0 or more strings , such as" Cn/*/config.xml "will match" cn/javass/config.xml "but does not match" Cn/config.xml "and" Cn/config-*.xml "will match" Cn/config-dao.xml ";
"* * ": matches 0 or more directories in the path , such as" Cn/**/config.xml "will match" Cn/config.xml ", also matches" Cn/javass/spring/config.xml ";" cn/javass/ Config-**.xml "will match" cn/javass/config-dao.xml ", i.e." * * "as two" * "processing.
Spring provides antpathmatcher for Ant-style path matching. Please refer to cn.javass.spring.chapter4. Antpathmatchertest for specific tests.
Spring supports loading a resource in addition to the prefix "Classpath:" When loading a classpath resource, and also provides a prefix "classpath*:" To support loading all matching classpath resource.
Spring provides the Resourcepatternresolver interface to load multiple Resource, which inherits Resourceloader and adds "resource[" Getresources (String Locationpattern) "is used to load multiple resource:
Public Interface extends Resourceloader { = "classpath*:"; throws IOException;
Spring provides a resourcepatternresolver implementation pathmatchingresourcepatternresolver, which is based on pattern matching, which uses antpathmatcher for path matching by default. In addition to supporting Resourceloader supported prefixes, it also supports "classpath*:" To load all matching classpath Resource,resourceloader does not support the prefix "classpath*:":
First do the preparation, create the "Meta-inf" directory in the Project "Resources", and then create a "INDEX" under it. LIST "file. At the same time in "Org.springframework.beans-3.0.5.release.jar" and "Org.springframework.context-3.0.5.release.jar" The same directories and files exist in the two jar packages. Then create a "LICENSE" file that exists in "Com.springsource.cn.sf.cglib-2.2.0.jar".
First, "Classpath ": used to load one and only one resource in the classpath (including Jar packages), and only one for multiple matches, so consider the" classpath*: "prefix if more than one match is required;
@Test Public voidTestclasspathprefix ()throwsIOException {resourcepatternresolver resolver=NewPathmatchingresourcepatternresolver (); //only one absolute match resource is loaded and loaded via Resourceloader.getresourceResource[] Resources=resolver.getresources ("Classpath:meta-inf/index. LIST "); Assert.assertequals (1, resources.length); //only one matching resource is loaded and loaded via Resourceloader.getresourceresources = Resolver.getresources ("classpath:meta-inf/*. LIST "); Assert.asserttrue (Resources.length= = 1); }
second, "classpath* ": used to load all matching resources in the classpath (including Jar packages). Classpath with wildcards uses the "Enumeration<url> getresources (String name)" Method of "ClassLoader" to find resources before wildcards, and then to obtain matching resources through pattern matching. such as "classpath:meta-inf/*. List "will first load the wildcard before the directory" Meta-inf ", and then traverse the path for sub-path matching to obtain matching resources.
@Test Public voidTestclasspathasteriskprefix ()throwsIOException {resourcepatternresolver resolver=NewPathmatchingresourcepatternresolver (); //will load multiple absolute matches for all resource//the non-modal path section is first loaded via Classloader.getresources ("Meta-inf")//then the traversal pattern matchesResource[] Resources=resolver.getresources ("Classpath*:meta-inf/index. LIST "); Assert.asserttrue (Resources.length> 1); //multiple pattern-matching resource will be loadedresources = Resolver.getresources ("classpath*:meta-inf/*. LIST "); Assert.asserttrue (Resources.length> 1); }
Note the "Resources.length >1" description returns multiple resource. Matches are returned, regardless of pattern or non-pattern matches.
Contains "Asm-license.txt" file in "Com.springsource.cn.sf.cglib-2.2.0.jar", for loading resources using "classpath*: Asm-*.txt" To load a resource will not load anything " Asm-license.txt "file, note that it must be pattern path matching before you experience this problem. This is due to the limitation of the "Getresources (String Name)" Method of "ClassLoader", and for the case of name "", only the classpath of the file system will be returned, and the jar package root path will not be shifting.
@Test Public voidTestclasspathasteriskprefixlimit ()throwsIOException {resourcepatternresolver resolver=NewPathmatchingresourcepatternresolver ();//the directory will be loaded first through Classloader.getresources (""),//will return only the classpath of the file system without returning the path of the jar//then the traversal pattern matchesresource[] Resources = resolver.getresources ("Classpath*:asm-*.txt"); Assert.asserttrue (Resources.length= = 0); //will be loaded via Classloader.getresources ("Asm-license.txt")//Asm-license.txt exists in Com.springsource.net.sf.cglib-2.2.0.jarresources = resolver.getresources ("Classpath*:asm-license.txt"); Assert.asserttrue (Resources.length> 0); //will load only the file system classpath matching resourceresources = resolver.getresources ("classpath*:licens*"); Assert.asserttrue (Resources.length= = 1); }
For "Resolver.getresources" ("Classpath*:asm-*.txt"), "the 0 resources should be returned because they are not in the project" Resources "directory;" Resolver.getresources (" Classpath*:asm-license.txt ");" The resource in the jar package will be returned, "Resolver.getresources (" classpath*:licens* "), because only the file system Classpath resource will be returned, so 1 resources are returned.
Therefore, when you load a wildcard path (that is, the path contains a wildcard character), you must include a root directory to ensure that the loaded resources are all, not parts.
third, "File ": loads the resource in one or more file systems. If "File:d:/*.txt" will return all TXT files under D disk;
Four, no prefix : through the Resourceloader implementation of loading a resource.
The Getresources method provided by Appliacationcontext will get the resource delegated to the Resourcepatternresolver implementation, using Pathmatchingresourcepatternresolver by default. All you need to do here is not to describe how to use it.
4.4.2 Injection Resource Array
Spring also supports injecting a resource array, directly looking at the configuration as follows:
<BeanID= "ResourceBean1"class= "Cn.javass.spring.chapter4.bean.ResourceBean4"> < Propertyname= "Resources"> <Array> <value>Cn/javass/spring/chapter4/test1.properties</value> <value>Log4j.xml</value> </Array> </ Property> </Bean> <BeanID= "ResourceBean2"class= "Cn.javass.spring.chapter4.bean.ResourceBean4"> < Propertyname= "Resources"value= "Classpath*:meta-inf/index." LIST "/> </Bean> <BeanID= "ResourceBean3"class= "Cn.javass.spring.chapter4.bean.ResourceBean4"> < Propertyname= "Resources"> <Array> <value>Cn/javass/spring/chapter4/test1.properties</value> <value>Classpath*:meta-inf/index. LIST</value> </Array> </ Property> </Bean>
"ResourceBean1" does not need to introduce more, the traditional way of implementation; for "resourceBean2" then use the prefix "classpath*", see this everyone should understand, load matching multiple resources; "RESOURCEBEAN3" is a mixed use;
Spring uses Resourcearraypropertyeditor for type conversion, and it defaults to using "Pathmatchingresourcepatternresolver" to parse the path to the resource object. As long as everyone will use "pathmatchingresourcepatternresolver", some other implementations are entrusted to it, such as Appliacationcontext's "getresources" method.
Open Tao Spring3 (4.4)-Resource 4.4 Resource wildcard path