What are the roles and differences between Beanfactory and ApplicationContext in spring?

Source: Internet
Author: User

Beanfactory Class Relationship inheritance diagram

1. Beanfactory Type structure System:

The Beanfactory interface and its subclasses define the spring IOC container architecture, because the beanfactory system is very large and complex, so to understand the spring IOC, you need to clarify the Beanfactory's successor authority.

2. ApplicationContext's structural system:

The ApplicationContext interface is the most commonly used IOC container in spring that encapsulates more functionality on a beanfactory, which contains two sub-interfaces: Configurableapplicationcontext, Webapplicationcontext.

Configurableapplicationcontext has the following structural system:

The detailed structural system is as follows:

The A.abstractapplicationcontext structure system is as follows:

The B.configurableportletapplicationcontext architecture is as follows:

The C.configurablewebapplicationcontext structure system is as follows:

2). The Webapplicationcontext architecture is as follows:

Spring's IOC container is an instantiated class that implements the Beanfactory interface. In fact, Spring provides two different kinds of containers: one is the most basic beanfactory, the other is the extended applicationcontext. Beanfactory provides only the most basic dependency injection support, while ApplicationContext expands the beanfactory to provide additional functionality. The two are also very different from the initialization of the bean. Beanfactory reads configuration information when it needs to be invoked to generate an instance of a class. If a read-in Bean is configured correctly, errors in other configurations will not affect the program's operation. ApplicationContext, when initialized, reads the XML configuration information into memory, verifies the XML file, and, if there is no error in the configuration file, creates all the beans and serves the application directly. The only disadvantage relative to the basic beanfactory,applicationcontext is the memory footprint. When the application configuration bean is large, the program starts slowly.

ApplicationContext uses the Java reflection mechanism to automatically identify the beanpostprocessor defined in the configuration file, Instantiationawarebeanpostprocessor and Beanfactorypostprocessor, and automatically register them in the application context , and beanfactory needs to be registered in code by calling the Addbeanpostprocessor () method manually.

In Beanfactory, many functions need to be implemented programmatically, while in ApplicationContext they can be configured in the same way.

Role:

1. The beanfactory is responsible for reading the bean configuration document, managing the bean's loading, instantiating, maintaining the dependency between the beans, and responsible for the bean's declaration cycle.
2. ApplicationContext provides more complete framework functionality in addition to the functionality provided by the beanfactory above:

A. Internationalization support
B. Resource access: Resource rs = ctx. GetResource ("Classpath:config.properties"), "File:c:/config.properties"
C. Event delivery: By implementing the Applicationcontextaware interface
3. Common ways to get ApplicationContext:
Filesystemxmlapplicationcontext: Created from a file system or URL-specified XML configuration file with an array of configuration file names or file names
Classpathxmlapplicationcontext: Created from the classpath XML configuration file, you can read the configuration file from the jar package
Webapplicationcontextutils: Reading a configuration file from the Web application's root directory requires a configuration in XML. config, which can be configured with a listener or servlet to implement
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Either way, the default profile is Web-inf/applicationcontext.xml, or you can use Context-param to specify the configuration file
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>

What are the roles and differences between Beanfactory and ApplicationContext 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.