Load File Order
Scenario One: use Classpath to load and do not contain wildcard characters
This is the simplest case in which spring uses the ClassLoader GetResource method of the current thread to get the URL of the resource, and if the ClassLoader of the current thread cannot be obtained, Spring will use the ClassLoader that loads the class org.springframework.util.ClassUtils. 1. When the project directory structure is shown in the figure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/application-context.xml");
Load [Conf/application-context.xml] 2. When the project directory structure is as follows: The bin directory is only a. class file, there are no configuration files, and there are configuration files in the dependent Conf.jar:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/application-context.xml");
Load [Conf/application-context.xml] 3. When the project directory structure is shown in the figure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/application-context.xml");
Only the Application-context.xml file in the bin/conf directory will be loaded and the Conf/application-context.xml in the jar package will not be loaded. Scenario Two: loading with classpath, including wildcard characters
Spring determines the approximate location of the resource by using the non-wildcard part of the path, and then determines the specific resource location 1 based on the location. When the project directory structure is shown in the figure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/**/*application-context.xml");
Load [Admin-application-context.xml]
Load [Application-context.xml] 2. When the project directory structure is shown in the figure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/**/*application-context.xml");
Load Conf/application-context.xml
Load Conf/admin/admin-application-context.xml 3. When the project directory structure is shown in the figure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/**/*application-context.xml");
Bin/conf/application-context.xml files and Bin/conf/admin/admin-application-context.xml will be loaded,
However, the configuration files in the Conf.jar file are not loaded. Scenario Three: Use the classpath* prefix and do not include a wildcard character
Use the classpath* prefix to get all classpath resources that match a given path, avoiding the occurrence of two files with the same name in different locations, and spring loading only one of them. When the project directory structure is shown in the figure:
This is used
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath*:conf/application-context.xml");
Spring will load the Application-context.xml file in the bin directory and the Application-context.xml file in the jar package. scenario Four: Using the classpath* prefix, including wildcard characters
When the project directory structure is shown in the figure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath*:conf/**/*application-context.xml");
The Conf directory includes all of the configuration files in all levels of subdirectories, so bin/conf/application-context.xml and Bin/conf/admin/admin-application-context.xml
And the Conf/application-context.xml and Conf/admin/admin-application-context.xml in the jar package will be loaded classpath* Loading and classpath loading differences
classpath*: Appears to load the same file from Classpath and multiple jar files, Classpath: Only the first file found is loaded.
Classpath* the use of the ClassLoader getresources () method;
In the Pathmatchingresourcepatternresolver class, we can better understand the handling of its pair: if it starts with classpath*, it traverses classpath.
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.
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); }