Spring can load files from classpath, such as the Bean's definition file, by specifying classpath*: with the classpath: prefix plus path. classpath*: Appears to load the same file from multiple jar Files. classpath: only the first file found is Loaded.
For example, the package ' com.test.rs ' in Resource1.jar has a ' jarappcontext.xml ' file that reads as Follows:
<bean name= "processorimpla" class= "com.test.spring.di.ProcessorImplA"/>
The package ' com.test.rs ' in Resource2.jar also has a ' jarappcontext.xml ' file that reads as Follows:
<bean id= "processorimplb" class= "com.test.spring.di.ProcessorImplB"/>
You can load the files in the two jar packages by using the following code
ApplicationContext CTX = new Classpathxmlapplicationcontext ("classpath*:com/test/rs/jarappcontext.xml");
If you write the following code, you can only find one of the XML files (the order depends on the load order of the jar Packages)
ApplicationContext CTX = new Classpathxmlapplicationcontext ("classpath:com/test/rs/jarappcontext.xml");
Classpath*: is used for multiple component (eventually released into different jar packages) in parallel development, the respective bean definition files according to certain rules: package+filename, The callers who use these component can load the files in.
Classpath*: loading uses the ClassLoader getResources()
method, and if it is running on a different Java EE server, they may behave differently when working with the jar file because the application server provides its own ClassLoader Implementation. To test classpath*:
Whether it is valid, you can test it with ClassLoader from the jar file in the classpath to load the file: getClass().getClassLoader().getResources("<someFileInsideTheJar>")
. (the example above is the state running in Sun's Jre)
From Spring's source code, in the Pathmatchingresourcepatternresolver class, we can better understand its handling: if it starts with classpath*, it will traverse Classpath.
[java]View PlainCopy
- Protected resource[] findallclasspathresources (String Location) throws IOException {
- String Path = location;
- if (path.startswith ("/")) {
- Path = path.substring (1);
- }
- Enumeration resourceurls = getClassLoader (). getresources (path);
- set<resource> result = new linkedhashset<resource> (16);
- while (resourceurls.hasmoreelements ()) {
- URL url = (url) resourceurls.nextelement ();
- Result.add (convertclassloaderurl (url));
- }
- return Result.toarray (new Resource[result.size ()));
- }
http://blog.csdn.net/kkdelta/article/details/5560210, introduces all the matching names found in Java traversal classpath.
In addition, when loading resource, the meanings of the other prefixes are as follows: note that classpath* can only be used with the path specified by the configuration file and cannot be used in parameters used for getresource. such as Appcontext.getresource (" Classpath*:conf/bfactoryctx.xml ") will be abnormal.
prefix |
Example |
Description |
Classpath |
classpath:com/myapp/config.xml
|
Loaded from the Classpath. |
File |
file:/data/config.xml
|
As URL loaded from the file System. |
http |
http://myserver/logo.png
|
As URL Load. |
(none) |
/data/config.xml
|
According ApplicationContext to the Judgment. |
|
From the source of spring can see the reason: if it is classpath: start, load from classpath, otherwise try the url, if failed, call Getresourcebypath
[java]View PlainCopy
- Public Resource GetResource (String Location) {
- Assert.notnull (location, "must is not null");
- if (location.startswith (classpath_url_prefix)) {
- return new Classpathresource (location.substring (classpath_url_prefix.length ()), getclassloader ());
- }
- else {
- try {
- //Try to parse the location as a URL ...
- URL url = new URL (location);
- return New Urlresource (url);
- }
- catch (malformedurlexception Ex) {
- //No URL, Resolve as Resource path.
- return Getresourcebypath (location);
- }
- }
- }
Getresourcebypath will be overwritten by different ApplicationContext
Implementations.
If the genericwebapplicationcontext is covered as follows:
[java]View PlainCopy
- Protected Resource Getresourcebypath (String Path) {
- return new Servletcontextresource (this.servletcontext, path);
- }
- If the filesystemxmlapplicationcontext is covered as follows:
- Protected Resource Getresourcebypath (String Path) {
- if (path! = null && path.startswith ("/")) {
- Path = path.substring (1);
- }
- return New Filesystemresource (path);
- }
It is still common in Java to read files when loading from a file:
The way to get InputStream as Classpathresource is to use class Loader.
[java]View PlainCopy
- Public InputStream getInputStream () throws IOException {
- InputStream is;
- if (this.clazz! = Null) {
- is = This.clazz.getResourceAsStream (this.path);
- }
The way to get inputstream, as filesystemresource, is to use Fileinputstream.
Public InputStream getInputStream () throws IOException {
return new FileInputStream (this.file);
}
The way to get inputstream, as servletcontextresource, is to use Servletcontext.getresourceasstream.
[java]View PlainCopy
- public inputstream getinputstream () throws ioexception {
- inputstream is = this.servletcontext.getresourceasstream ( Span class= "keyword" >this.path)
- if (is == null) {
- throw new filenotfoundexception ( "could not open " + getdescription () );
- }
- return is;
- }
How to find in classpath:spring