Six ways to load XML configuration files in spring

Source: Internet
Author: User

Six ways to load XML configuration files in spring Blog Category:
    • Spring&ejb
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
  1. <context-param>
  2. <!--Context Configuration locations for Spring XML files ----
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>
  5. ./web-inf/**/appserver-resources.xml,
  6. Classpath:config/aer/aercontext.xml,
  7. Classpath:org/codehaus/xfire/spring/xfire.xml,
  8. ./web-inf/**/*.spring.xml
  9. </param-value>
  10. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.