Resource 4.4 Resource Wildcard path (pick)

Source: Internet
Author: User

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" use the prefix "classpath*", see what this people should understand, load matching multiple resources; "RESOURCEBEAN3" is a mixed use The test code is in "Cn.javass.spring.chapter4.ResourceInjectTest.testResourceArrayInject".

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.

4.4.3 Appliacationcontext enables support for a variety of resource

First, Classpathxmlapplicationcontext : The default is to load the return Classpathresource via Classpath, providing a class two constructor method:

 Public classClasspathxmlapplicationcontext {//1) Obtain resources according to Configlocation by Resourcepatternresolver        PublicClasspathxmlapplicationcontext (String configlocation);  PublicClasspathxmlapplicationcontext (String ... configlocations); PublicClasspathxmlapplicationcontext (string[] configlocations, ...); //2) Direct return to Classpathresource by direct path        PublicClasspathxmlapplicationcontext (String path, Class clazz);  PublicClasspathxmlapplicationcontext (string[] paths, Class clazz);  PublicClasspathxmlapplicationcontext (string[] paths, Class clazz, ...); }  

The first class constructor is used to obtain resources by matching using the "getresources ()" Interface of "Resourcepatternresolver" based on the provided configuration file path; "Classpath:config.xml"

The second class constructor constructs the Classresource resource based on the provided path and Clazz. The "Public Classpathresource (String path, class<?> clazz)" constructor is used to obtain the resource.

Second, Filesystemxmlapplicationcontext: will load the resources relative to the current working directory "configlocation" location, note on the Linux system regardless of " Configlocation "with"/"as a relative path, while on a window system such as" D:/resourceinject.xml "is an absolute path. Therefore, it is not recommended to use the ApplicationContext unless it is necessary.

 Public class filesystemxmlapplicationcontext{         public  Filesystemxmlapplicationcontext (String configlocation);           Public Filesystemxmlapplicationcontext (String ... configlocations,......);  }
// Linux Systems, the following are all loaded  relative to the current VM path New Filesystemxmlapplicationcontext ("Chapter4/config.xml");   New Filesystemxmlapplicationcontext ("/chapter4/confg.xml");  
// Windows System, the first one will be loaded relative to the current VM  path; // the second one is the absolute path mode  load New Filesystemxmlapplicationcontext ("Chapter4/config.xml");   New Filesystemxmlapplicationcontext ("D:/chapter4/confg.xml");

It is also important to note that the constructor uses a relative path on a Linux system, and the Ctx.getresource () method will return a relative path resource if it starts with a "/" to obtain an absolute path resource without a leading "/". As follows:

// Linux System, the first one will be loaded relative to the current VM  path; // the second one is the absolute path mode  load Ctx.getresource ("Chapter4/config.xml");  Ctx.getresource ("/root/confg.xml");   // Windows System, the first one will be loaded relative to the current VM  path; // the second one is the absolute path mode  load Ctx.getresource ("Chapter4/config.xml");  Ctx.getresource ("D:/chapter4/confg.xml");  

Therefore, if you need to load an absolute path resource, it is best to choose the prefix "file", which will all be loaded according to absolute path. As in the Linux system "Ctx.getresource (" File:/root/confg.xml ");"

Resource 4.4 Resource Wildcard path (pick)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.