A simple test with applicationcontext, get the Bean instance (object) defined in spring. You can use:
ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Registerdao Registerdao = (Registerdao) ac.getbean ("Registerdao");
If it is more than two:
ApplicationContext ac = new Classpathxmlapplicationcontext (new string[]{"Applicationcontext.xml", "Dao.xml"});
Or use wildcard characters:
ApplicationContext ac = new Classpathxmlapplicationcontext ("Classpath:/*.xml");
Second, classpathxmlapplicationcontext[can only read the configuration files placed in the Web-info/classes directory] and the difference between filesystemxmlapplicationcontext
Classpath: prefix is not required, default refers to the project's classpath path below;
If you want to use an absolute path, you need to add a file: prefix to indicate that this is an absolute path;
For Filesystemxmlapplicationcontext:
The default represents two types:
1. No drive letter is the project work path, that is, the root directory of the project;
2. A drive symbol indicates the absolute path of the file.
If you want to use the classpath path, prefix classpath is required:
public class Helloclient {
Protected static final Log log = Logfactory.getlog (Helloclient.class);
public static void Main (string[] args) {
Resource Resource = new Classpathresource ("Appcontext.xml");
Beanfactory factory = new Xmlbeanfactory (Resource);
Using the Classpath path
ApplicationContext factory = new Classpathxmlapplicationcontext ("Classpath:appcontext.xml");
ApplicationContext factory = new Classpathxmlapplicationcontext ("Appcontext.xml");
Classpathxmlapplicationcontext uses a file prefix that can use an absolute path.
ApplicationContext factory = new Classpathxmlapplicationcontext ("File:f:/workspace/example/src/appcontext.xml");
The path to the file system, which is the root path of the project by default
ApplicationContext factory = new Filesystemxmlapplicationcontext ("Src/appcontext.xml");
ApplicationContext factory = new Filesystemxmlapplicationcontext ("Webroot/web-inf/appcontext.xml");
The classpath: prefix is used so that filesystemxmlapplicationcontext can also read the relative path under Classpath
ApplicationContext factory = new Filesystemxmlapplicationcontext ("Classpath:appcontext.xml");
ApplicationContext factory = new Filesystemxmlapplicationcontext ("File:f:/workspace/example/src/appcontext.xml") ;
//Do not add the file prefix
applicationcontext factory = new Filesystemxmlapplicationcontext ("F:/workspace/example/src/appcontext.xml");
Ihelloworld hw = (ihelloworld) factory.getbean ("Helloworldbean");
Log.info (hw.getcontent ("Luoshifei"));
}
}