Since we are currently working on a project where a requirement is that all functions are loaded into the system in the form of plug-ins, it is necessary to use spring to dynamically load the configuration file under a certain location, so we summarize the way in which the XML configuration file is loaded in spring, and I summarize 6 kinds of XML is the most common spring application configuration source. Several containers in spring support the use of XML assembly beans, including:
Xmlbeanfactory,classpathxmlapplicationcontext,filesystemxmlapplicationcontext,xmlwebapplicationcontext
One: xmlbeanfactory reference Resources
Resource Resource = new Classpathresource ("Appcontext.xml");
Beanfactory factory = new Xmlbeanfactory (Resource);
Two: Classpathxmlapplicationcontext compilation path
ApplicationContext factory=new classpathxmlapplicationcontext ("Classpath:appcontext.xml");
In the SRC directory
ApplicationContext factory=new classpathxmlapplicationcontext ("Appcontext.xml");
ApplicationContext factory=new Classpathxmlapplicationcontext (new string[] {"Bean1.xml", "Bean2.xml"});
Under the src/conf directory
ApplicationContext factory=new classpathxmlapplicationcontext ("Conf/appcontext.xml");
ApplicationContext factory=new classpathxmlapplicationcontext ("File:g:/test/src/appcontext.xml");
Three: Using the file system path
ApplicationContext factory=new filesystemxmlapplicationcontext ("Src/appcontext.xml");
The classpath: prefix is used as a flag, so that Filesystemxmlapplicationcontext can also read the relative path in Classpath
ApplicationContext factory=new filesystemxmlapplicationcontext ("Classpath:appcontext.xml");
ApplicationContext factory=new filesystemxmlapplicationcontext ("File:g:/test/src/appcontext.xml");
ApplicationContext factory=new filesystemxmlapplicationcontext ("G:/test/src/appcontext.xml");
Four: Xmlwebapplicationcontext is specifically tailored for Web engineering.
ServletContext ServletContext = Request.getsession (). Getservletcontext ();
ApplicationContext CTX = Webapplicationcontextutils.getwebapplicationcontext (ServletContext); 
V: Use Beanfactory  
Beandefinitionregistry reg = new Defaultlistablebeanfactory ();  
Xmlbeandefinitionreader reader = new Xmlbeandefinitionreader (reg);  
Reader.loadbeandefinitions (New Classpathresource ("Bean1.xml"));  
Reader.loadbeandefinitions (New Classpathresource ("Bean2.xml"));  
beanfactory bf= (beanfactory) reg;
VI: Load multiple profiles when Web app starts  
Multiple configuration files can also be loaded via Contextloaderlistener, using in the Web. xml file;
< The context-pararn> element specifies multiple configuration file locations, which are configured as follows: &NBSP;
Java code
- <context-param>
- <!--Context Configuration locations for Spring XML files ----
- <param-name>contextConfigLocation</param-name>
- <param-value>
- ./web-inf/**/appserver-resources.xml,
- Classpath:config/aer/aercontext.xml,
- Classpath:org/codehaus/xfire/spring/xfire.xml,
- ./web-inf/**/*.spring.xml
- </param-value>
- </context-param>
This method loads the configuration file as long as you already know where the configuration file is, although you can take advantage of the "*" wildcard character, but with limited flexibility.
Six ways to load XML configuration files in spring