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 a file prefix
ApplicationContext factory = new Filesystemxmlapplicationcontext ("F:/workspace/example/src/appcontext.xml");
Ihelloworld HW = (ihelloworld) factory.getbean ("Helloworldbean");
Log.info (Hw.getcontent ("Luoshifei"));
}
}
1. Filesystemxmlapplicationcontext
Pplicationcontext CTX = new Filesystemxmlapplicationcontext ("Bean.xml"); Load a single configuration file
String[] Locations = {"Bean1.xml", "Bean2.xml", "Bean3.xml"};
ApplicationContext CTX = new Filesystemxmlapplicationcontext (locations); Load multiple configuration files
ApplicationContext ctx =new filesystemxmlapplicationcontext ("D:roject/bean.xml");//loading files according to specific path
2. Classpathxmlapplicationcontext
Pplicationcontext CTX = new Classpathxmlapplicationcontext ("Bean.xml");
String[] Locations = {"Bean1.xml", "Bean2.xml", "Bean3.xml"};
ApplicationContext CTX = new Classpathxmlapplication (locations);
Note: where Filesystemxmlapplicationcontext and Classpathxmlapplicationcontext are located in the same way as the Beanfactory XML file location is path-based.
3. Xmlwebapplicationcontext
ServletContext ServletContext = Request.getsession (). Getservletcontext ();
ApplicationContext CTX = Webapplicationcontextutils.getwebapplicationcontext (ServletContext);
Spring MVC uses the Classpathxmlapplicationcontext or Filesystemxmlapplicationcontext and Xmlwebapplicationcontext classes for operations where Xmlwebapplicationcontext is specifically tailored for Web engineering.