We all know that Java reads ordinary files through basic I/O InputStream, OutStream, Reader, Writer and so on. In the spring framework, how does it identify the XML configuration file?
This depends on the two interfaces of the IOC container beanfactory and applicationcontext:
beanfactory Interface
|--------Xmlbeanfactory (Implementation Class)
ApplicationContext Interface
|--------Classpathxmlapplicationcontext (Implementation Class)
|---------Filesystemxmlapplicationcontext (Implementation Class)
|---------Xmlwebapplicationcontext (Implementation Class)
Beanfactory is an interface that spring uses to instantiate, configure, and manage objects with a Getbean () method that provides only the most basic features of spring, typically used in low-memory situations, such as applets. The generic spring project uses its derived class ApplicationContext, which automatically parses our configured XML file and then, based on the configured Bean, puts the new object into a map, the key is the Bean's ID, and the value is the object of new.
1. compile the path classpathxmlapplicationcontext to load the XML file (example http://blog.csdn.net/shymi1991/article/details/48085955 in our introductory article)
This way the XML file must be in the project's build path, generally with classpath: As a prefix, you can also omit
1) under/SRC directory
ApplicationContext factory=new classpathxmlapplicationcontext ("Classpath:appcontext.xml");
ApplicationContext factory=new classpathxmlapplicationcontext ("Appcontext.xml");
2)/src/conf directory
ApplicationContext factory=new classpathxmlapplicationcontext ("Conf/appcontext.xml");
3) load multiple XML files at the same time
ApplicationContext factory=new Classpathxmlapplicationcontext (new string[] {"Bean1.xml", "Bean2.xml"});
2. Path to the file system
An absolute path to an XML file, usually prefixed with file: or omitted
ApplicationContext factory=new filesystemxmlapplicationcontext ("File:g:/test/src/appcontext.xml");
ApplicationContext factory=new filesystemxmlapplicationcontext ("G:/test/src/appcontext.xml");
3. Xmlwebapplicationcontext is custom-built for Web engineering
ServletContext ServletContext = Request.getsession (). Getservletcontext ();
ApplicationContext CTX = Webapplicationcontextutils.getwebapplicationcontext (ServletContext);
How spring loads the XML configuration file ApplicationContext