How Spring loads xml configuration files: ApplicationContext

Source: Internet
Author: User

How Spring loads xml configuration files: ApplicationContext

Everyone knows that Java reads common files through InputStream, OutStream, Reader, and Writer in Basic I/O. In the spring framework, how does it identify the xml configuration file?

This depends on the two interfaces of the IoC container.BeanFactoryAndApplicationContext:

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. It has a getBean () method that only provides the most basic features of spring, it is generally used in the case of low memory, such as Applet. The general spring project uses its derived class ApplicationContext. This class will automatically parse the xml file we configured, and then use the configured bean to create a new object and put the new object into a Map, the key is the bean id, and the value is the new object.

 

1.Compilation pathClassPathXmlApplicationContext to load xml files (example http://blog.csdn.net/shymi1991/article/details/48085955 in our getting started)

In this way, the xml file must be in the build path of the Project. Generally, classpath: is used as the prefix and can be omitted.

1) under the/src directory

ApplicationContext factory = new ClassPathXmlApplicationContext (classpath: appcontext. xml );
ApplicationContext factory = new ClassPathXmlApplicationContext (appcontext. xml );

2) In the/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. File System Path

The absolute path of an xml file. Generally, file: is used as the prefix. It can also be omitted.

ApplicationContext factory = new FileSystemXmlApplicationContext (file: G:/Test/src/appcontext. xml );
ApplicationContext factory = new FileSystemXmlApplicationContext (G:/Test/src/appcontext. xml );

 

3. XmlWebApplicationContext is customized for Web engineering

ServletContext servletContext = request. getSession (). getServletContext ();
ApplicationContext ctx = WebApplicationContextUtils. getWebApplicationContext (servletContext );

 

??

 

Related Article

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.