Spring six ways to load XML

Source: Internet
Author: User

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;

Spring six ways to load XML

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.