Spring source reading: Design and implementation of IOC container (ii)--applicationcontext

Source: Internet
Author: User

In the previous topic, we learned about the basic concepts of the IOC container and how the beanfactory is designed and implemented, and here's how the ApplicationContext approach is implemented.

ApplicationContext

In spring's reference documentation, why would you recommend using ApplicationContext? What benefits can it bring to our applications? As one of the implementations of Beanfactory, how is it designed? How is the webapplictioncontext\xmlapplicationcontext used in SPRINGMVC associated with it?

ApplicationContext and Beanfactory:

From the class diagram, it can be seen that ApplicationContext is rendered as a sub-interface of the beanfactory. But it adds a new feature on the basis of Beanfactory: Messagesource (message support, such as internationalization), Applicationeventpublisher (event support), Resourcepatternresolver (parse Resource).

Abstractapplicationcontext

The realization class abstractapplicationcontext of applicationcontext has basically realized the function of ApplicationContext.

/*** Abstract Implementation of the {@linkorg.springframework.context.ApplicationContext} * interface. Doesn ' t mandate the type of storage used for configuration; Simply * implements common context functionality. Uses the Template Method design pattern, * requiring concrete subclasses to implement abstract methods. * * <p>in contrast to a plain beanfactory, an applicationcontext are supposed * to detect special beans defined in it s Internal Bean Factory: * Therefore, this class automatically registers * {@linkorg.springframework.beans.factory.config.BeanFactoryPostProcessor Beanfactorypostprocessors}, * {@linkorg.springframework.beans.factory.config.BeanPostProcessor Beanpostprocessors} * and {@linkOrg.springframework.context.ApplicationListener Applicationlisteners} * which is defined as beans in the context. * * <p>a {@linkOrg.springframework.context.MessageSource} may also is supplied * as a bean in the context, with the name "Messagesou Rce "; Otherwise, message * Resolution is delegated to the parent context. Furthermore, a multicaster * for application events can be supplied as "applicationeventmulticaster" bean * of type {@linkOrg.springframework.context.event.ApplicationEventMulticaster} * in the context; Otherwise, a default multicaster of type * {@linkOrg.springframework.context.event.SimpleApplicationEventMulticaster} 'll be used. * * <p>implements resource loading through extending * {@linkOrg.springframework.core.io.DefaultResourceLoader}. * Consequently treats Non-url resource paths as class path Resou RCEs * (Supporting full class path resource names this include the package path, * e.g. "Mypackage/myresource.dat"), Unles s the {@link#getResourceByPath} * method is overwritten in a subclass. */
View Code

The general meaning of the above description is:

1. This class is an abstract implementation of ApplicationContext, and does not specify which bean definition configuration policy to use. It accomplishes the universal implementation of various applicationcontext. When designing this class, the template method is used to design the pattern, and some methods need to be implemented in the specific subclass.

2, compared with beanfactory, Applicationcontxt can put beanfactorypostprocessor, beanpostprocessor, Applicationlistener is defined as a common bean.

3, in ApplicationContext can also use the Messagesource, use it when the name is "Messagesource". You can also use Applicationeventmulticaster (name is "Applicationeventmulticaster"), if not specified, The default is: Simpleapplicationeventmulticaster.

4, through the inheritance Defaultresourceloader way to realize the load of the resource file.

Next look at the inheritance of this class:

It can be seen from the class diagram that Abstractapplicationcontext is divided into two main branches: · Abstractrefreshwebapplicationcontext (primarily for web environments)

· Abstractxmlapplicationcontext (primarily for non-web environments)

For Abstractrefreshwebapplicationcontext, there are two types of configuration categories: Use the annotation configuration or use an XML file configuration.

For Abstractxmlapplicationcontext, using XML as the bean-defined configuration file, the difference between its two subclasses is where to load the bean definition.

In the next section, you can see how applicationcontext initializes the IOC container.

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.