Load file order scenario one: Load with classpath with no wildcard characters
This is the simplest case where spring defaults to using the ClassLoader of getResource
方法获取资源的
URL
,如果无法获得当前线程的
ClassLoader
,
Spring
将使用加载类
the current thread Org.springframework.util.ClassUtils's ClassLoader. 1. When the project directory structure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/application-context.xml");
Load [Conf/application-context.xml] 2. When the project directory structure:
That is, there are only. class files in the bin directory, 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:
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 based on the location
1. When the project directory structure:
ApplicationContext context = new Classpathxmlapplicationcontext ("Conf/**/*application-context.xml");
Load [Admin-application-context.xml]
Load [Application-context.xml] 2. When the project directory structure:
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:
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:
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:
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
As well as 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);}
Spring Load XML