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