How spring loads the XML configuration file ApplicationContext

Source: Internet
Author: User

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

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.