Introduction to Filesystemxmlapplicationcontext, Classpathxmlapplicationcontext and Xmlwebapplicationcontext

Source: Internet
Author: User

Reference: http://blog.csdn.net/souichiro/article/details/6068552

Today, I encountered a problem with spring, and I was prompted to find the Applicationcontext.xml file. It is not appropriate to invoke the method when loading this file, so the program cannot find the XML configuration file under the project.

There are three common ways to load a context file:

1, Filesystemxmlapplicationcontext

This method loads the configuration file from the file's absolute path, for example:

ApplicationContext CTX = new Filesystemxmlapplicationcontext ("G:/test/applicationcontext.xml");

If the parameter does not write the absolute path, then the method call will default to the absolute path to find, I test the time to find the default absolute path is the path where eclipse.

With absolute path, the flexibility of the program is very poor, so this method is generally not recommended.

(If you want to use the classpath path, you need to add the prefix classpath:)

2, Classpathxmlapplicationcontext

The method is to load the configuration file from Classpath (suitable for relative path loading), for example:

ApplicationContext CTX = new Classpathxmlapplicationcontext ("/applicationcontext.xml");

The method parameter in the classpath: prefix is not required, the default refers to the project's classpath path, which means that the default root directory is classpathxmlapplicationcontext when the web-inf/ Classes below, not the project root directory. This needs attention!

3, Xmlwebapplicationcontext

A method specifically tailored for Web engineering, recommended for use in Web projects. For example:

ServletContext ServletContext = Request.getsession (). Getservletcontext ();

ApplicationContext CTX = Webapplicationcontextutils.getwebapplicationcontext (ServletContext);

Introduction to Filesystemxmlapplicationcontext, Classpathxmlapplicationcontext and Xmlwebapplicationcontext

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.