Six ways to load XML configuration files in spring
Blog Category:
Xmlspringwebbeanblog because a project is currently in progress, one requirement in the project is that all functions are loaded into the system in the form of plug-ins, which requires the use of spring to dynamically load a configuration file under a certain location. So we summarize the way the XML configuration file is loaded in spring, and I've summed up 6 of them, and 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;
Six: Load multiple profiles when Web app starts
Multiple configuration files can also be loaded via Contextloaderlistener, using the Web. xml file
<context-pararn> element to specify multiple configuration file locations, which are configured as follows:
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 it knows where the configuration file is, although it can take advantage of the "*" wildcard character, but with limited flexibility.
Six ways to load XML configuration files in spring