Spring resource loading (classpath)

foreword

  Spring provides a powerful mechanism for loading resources. You can access the corresponding resources only through the special identifier of the resource address. It can not only identify different resource types through the resource address prefixes such as "classpath" and "file", but also supports Ant grid. The wildcarded resource address.

Resource address prefix

classpath:

Example:  classpath:resources/applicationContext.xml
Description:  Loading resources from the classpath, the cost of classpath: and classpath:/ are relative to the root path of the class. Resource files can be in standard filesystems or in JAR or ZIP class packages.
Added:  In the following maven project, the resource path
classpath:resources/applicationContext.xml points to spring's configuration resource file applicationContext.xml.

write picture description here

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:resources/applicationContext.xml");

file:

Example:  file:/conf/com/dmw/beanfactory/beans.xml
Description:  Use UrlResources to load resources from the file system directory, using absolute or relative paths.

http://

Example:   http://www.dmw.com/resource/beans.xml
Description:  Use UrlResource to load resources from the Web server.

ftp://

Example:   ftp://www.dmw.com/resource/beans.xml
Description:  Use UrlResource to load resources from the FTP server.

no prefix

Example:  com/dmw/beanfactory/beans.xml
Description:  Use the corresponding type of Resource according to the specific implementation class of ApplicationContext.

Ant-style resource address

Ant-style resource addresses support 3 matchers.
?: Matches a character in the filename.
*: matches any character in the filename.
**: matches multi-level paths.

classpath:com/t?st.xml:
  Matches com/test.xml, com/tast.xml or com/txst.xml files under the com classpath.

file:D:/conf/*.xml:
  matches all files with .xml suffix in the D:/conf directory of the file system.

Classpath:com/**/test.xml:
  Match the test.xml file under the com classpath (the current directory and its descendant directories).

Classpath:org/springframework/ */ .xml:
  Matches all files suffixed with .xml under the classpath org/springframework.

Classpath:org/**/servlet/bla.xml:
  matches not only classpath org/springframework/servlet/bla.xml, but also org/springframework/testing/servlet/bla.xml, but also org/servlet/bla.xml

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325581231&siteId=291194637