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
- Beanfactory AC =NewClasspathxmlapplicationcontext ("Applicationcontext. xml");
- Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath: applicationcontext. xml");
- Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath:/applicationcontext. xml");
- 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
- Beanfactory AC =NewClasspathxmlapplicationcontext (NewString [] {"Applicationcontext. xml",ApplicationContext-test.xml"});
- Beanfactory AC =NewClasspathxmlapplicationcontext ("Classpath *:/applicationcontext *. xml");
- 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
- Beanfactory factory =NewFilesystemxmlapplicationcontext ("Webroot/WEB-INF/applicationcontext. xml");
- 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
- 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
- Beanfactory factory =NewFilesystemxmlapplicationcontext (NewString [] {"Webroot/WEB-INF/applicationcontext. xml","Webroot/WEB-INF/applicationContext-test.xml"});
- Beanfactory factory =NewFilesystemxmlapplicationcontext ("Webroot/WEB-INF/applicationcontext *. xml");
- Beanfactory factory =NewFilesystemxmlapplicationcontext ("File: D:/workspace/testprojects/webroot/WEB-INF/applicationcontext *. xml");
Cited WEB-INF/classes under multiple files, is the same
Java code
- Beanfactory factory =NewFilesystemxmlapplicationcontext (NewString [] {"Classpath: applicationcontext. xml","Classpath: applicationcontext *. xml"});
- Beanfactory factory =NewFilesystemxmlapplicationcontext ("Classpath *: applicationcontext *. xml");
Black hair: http://heisetoufa.iteye.com/