Java EE load resource file and spring load XML file

Source: Internet
Author: User

Java EE load XML file

Resource interface, which is the interface used to load files.

Both Filesystemresource and Classpathresource implement the resource interface, which they can use to load XML files.

The specific code is as follows:

1 New Classpathresource ("file. xml"); 2 3 New Filesystemresource ("Drive letter:/project path/src/file. xml");

< Span class= "token p" >< Span class= "token lf" >classpathresource load file, from the source folder (such as SRC), to find the file.

< Span class= "token p" >< Span class= "token lf" > Note: Web project, Files of different source folders will eventually be compiled into the classes directory, so the files under the same homologous folder cannot have the same name, otherwise they will be overwritten.

Filesystemresource load file, is from the drive letter as the starting point, to find the file.

Span id= "wmd-input-section-5" class= "wmd-input-section" >spring load XML file

< Span class= "token lf" >< Span class= "token lf" > Resourceloader in spring is the interface for loading files.

< Span class= "token lf" >< Span id= "wmd-input-section-5" class= "wmd-input-section" >resourcepatternresolver interface inherits the Resourceloader interface.

< Span class= "token lf" > pathmatchingresourcepatternresolver class implemented Resourcepattern Resolver interface.

< Span class= "token lf" > spring loaded The code for the file is as follows:

1Resourcepatternresolver RP =Newpathmatchingresourcepatternresolver ();2 3 //Empty 1, here can be filled in classpath or File,classpath is the project SRC directory as the starting point, file is the drive letter as the starting point. 4 //empty 2, is the path + name of the file. 5Resource Resource = Rp.getresource ("Empty 1: Empty 2");6 7 8 //classpath*9 //empty 1 after adding *, such as classpath*, this means, in the path of the empty 2 under the name is empty 2 name of the file. The returned array. Ten //such as: Classpath*:com/bean.xml, from the source folder under the COM directory to find all the bean.xml, including the jar inside the COM directory under the file.  Oneresource[] Resources = rp.getresources ("Classpath*:applicationcontext.xml"); A  - //classpath:com/*.xml classpath:com/? XML - //? single character the //* Multiple Characters of 0 or more - //* * Hierarchical relationship (multi-level target) descendants directory found -resource[] resources1 = rp.getresources ("Classpath:com/bean*.xml"); -resource[] resources1 = rp.getresources ("Classpath:com/bean?. xml); +  - //look for the XML file in the COM directory and the XML file under the COM directory in the subdirectory. +resource[] resources1 = rp.getresources ("Classpath:com/*/bean.xml"); A  at //find XML files in the COM directory for all directories (descendants directory) under XML files and COM directories -resource[] resources1 = rp.getresources ("Classpath:com/**/bean.xml");

Java EE load resource file and spring load XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.