Ext.: http://blog.sina.com.cn/s/blog_a2f090ae0101e18d.html
http://blog.csdn.net/kkdelta/article/details/5507799
First, configure the listener listener in Web. XML to get spring to get it automatically. The following code is added:
<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class > </listener>
As long as it starts the service this configuration will default to load our configuration file, automatically initializes the beans inside. By default it will go to the/web-inf/applicationcontext.xml file. If you want to put the configuration file under Classpath, that is, the SRC directory, you need to specify the path, the specific code is as follows:
<!--Context Configuration locations for Spring XML files---
<context-param> <param-name> Contextconfiglocation</param-name>
<!--<param-value>/web-inf/applicationcontext-*.xml, Classpath*:applicationcontext-*.xml</param-value>
-<!--add your config file under SRC, The naming convention is: Applicationcontext-*.xml form, you can configure it in the following ways-
<param-value>classpath:applicationcontext-*.xml </param-value>
</context-param>
This way, when the server starts, it will automatically go to Classpa, which is the SRC directory, to find all the following: Applicationcontext-*.xml files and initialize them.
Spring Load Resource classpath*: Difference from classpath:
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*: The load uses the ClassLoader getresources () method, if it is run on a different Java EE server, because the application server provides its own ClassLoader implementation, They may behave differently when working with jar files. To test classpath*: valid, you can test it with ClassLoader from the jar file in Classpath: 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.
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> (+);
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 |
Loaded as a URL from the file system. |
http |
Http://myserver/logo.png |
Loaded as a URL. |
(none) |
/data/config.xml |
Judge according to ApplicationContext. |
|
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
Public Resource getresource (String location) {
assert.notnull (location, ' location must not ' be 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 covered by different applicationcontext implementations.
If the genericwebapplicationcontext is covered as follows:
Protected Resource Getresourcebypath (String path) {
return new Servletcontextresource (This.servletcontext, path);
}
The Filesystemxmlapplicationcontext overrides are as follows:
protected Resource Getresourcebypath (String path) {
if (path! = null & amp;& 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.
Public InputStream getInputStream () throws IOException {
inputstream are;
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.
Public InputStream getInputStream () throws IOException {
InputStream are = This.servletContext.getResourceAsStream ( This.path);
if (is = = null) {
throw new FileNotFoundException ("Could not open" + getdescription ());
}
return is;
}