Spring classpathxmlapplicationcontext and filesystemxmlapplicationcontext

Source: Internet
Author: User

From: http://heisetoufa.iteye.com/blog/1671306

First: classpathxmlapplicationcontext
This class, the default get is the path under the WEB-INF/classes/, that is, the path under the SRC of myeclipse, so use this is not to get the configuration file under the WEB-INF...

The following are some usage instructions to reference a file under SRC. The following methods are available:

Java Code
    1. Beanfactory AC =NewClasspathxmlapplicationcontext ("Applicationcontext. xml");
    2. Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath: applicationcontext. xml");
    3. Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath:/applicationcontext. xml");
    4. AC. getbean ("Beanid");


Classpath: What is the difference with classpath:
Classpath: Only one configuration file can be loaded. If multiple configuration files are configured, only the first configuration file can be loaded.
Classpath *: you can load multiple configuration files. If multiple configuration files exist, use this

Multiple files under SRC are cited in the following ways:

Java code
    1. Beanfactory AC =NewClasspathxmlapplicationcontext (NewString [] {"Applicationcontext. xml",ApplicationContext-test.xml"});
    2. Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath *:/applicationcontext *. xml");
    3. Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath *: src/folder/applicationcontext *. xml");

Besides: filesystemxmlapplicationcontext
This class obtains the project path by default, that is, the project name level. but I tested, in myeclipse, execute the main method, get the configuration file, path with webroot/WEB-INF/is no problem, but published to Tomcat, the configuration fails. However, you can only move all the configuration files to SRC, because there is a path problem after the configuration files are published to Linux.

Below are a few usage, cited a file under the WEB-INF, there are several ways

Java code
    1. Beanfactory factory =NewFilesystemxmlapplicationcontext ("Webroot/WEB-INF/applicationcontext. xml");
    2. Beanfactory factory =NewFilesystemxmlapplicationcontext ("File: D:/workspace/testprojects/webroot/WEB-INF/applicationcontext. xml");


The starting path of filesystemxmlapplicationcontext is the project path, so you can directly write webroot/WEB-INF
If file is added to the front, it indicates that the path to the back must be in the full path, which is the absolute path.

Below are a few usage, cited WEB-INF/classes under a file, there are the following methods

Java code
    1. Beanfactory factory =NewFilesystemxmlapplicationcontext ("Classpath: applicationcontext. xml");


In fact, this is the same as classpathxmlapplicationcontext.

Below are a few usage, cited under the WEB-INF of multiple files, there are the following methods

Java code
    1. Beanfactory factory =NewFilesystemxmlapplicationcontext (NewString [] {"Webroot/WEB-INF/applicationcontext. xml","Webroot/WEB-INF/applicationContext-test.xml"});
    2. Beanfactory factory =NewFilesystemxmlapplicationcontext ("Webroot/WEB-INF/applicationcontext *. xml");
    3. Beanfactory factory =NewFilesystemxmlapplicationcontext ("File: D:/workspace/testprojects/webroot/WEB-INF/applicationcontext *. xml");

Cited WEB-INF/classes under multiple files, is the same

Java code
    1. Beanfactory factory =NewFilesystemxmlapplicationcontext (NewString [] {"Classpath: applicationcontext. xml","Classpath: applicationcontext *. xml"});
    2. Beanfactory factory =NewFilesystemxmlapplicationcontext ("Classpath *: applicationcontext *. xml");

Black hair: http://heisetoufa.iteye.com/

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.