The life cycle of beanfactory and ApplicationContext in spring and their differences

Source: Internet
Author: User
Tags aop
Bean's life cycle

In many technologies, there is the concept of life cycle, such as the life cycle of activity, fragment, etc. in Android, with the life cycle of the servlet in the Web container. To be a high-level developer, you have to understand the life cycle in depth. Similarly, the bean in the spring container has a series of lifecycles, and to master Spring, it is necessary to master the Bean's life cycle in spring.

In spring, you can define a bean's life cycle from two levels: one is the scope of the bean, and the other is the series of stages that spring instantiates the bean. The following is an analysis of the bean's life cycle in Beanfactory and ApplicationContext respectively. the life cycle of beans in beanfactory 1. Life cycle Diagram

2, the specific process

Depending on the life cycle of the diagram, the process is as follows:

(1) When the caller calls Getbean (Beanname) to request a bean from the container, if the container registers the Instantiationawarebeanpostprocessor interface, before instantiating the bean, The Postprocessbeforeinstantiation () method of the interface is called.

(2) Invoke the Bean's constructor or factory method to instantiate the bean according to the configuration.

(3) If the container registers the Instantiationawarebeanpostprocessor interface, after the bean is instantiated, the Postprocessbeforeinstantiation () method of the interface is called, Some initialization settings are made on the instantiated object.

(4) If the bean is configured with attribute information, then the container sets the configured property in this step to the bean's corresponding property. The Postprocesspropertyvalues () method of the Instantiationawarebeanpostprocessor interface is called before the property is set.

(5) Call the Setxxx () method in the bean to set the property.

(6) If the bean implements the Beannameaware interface, the Setbeanname () method of the interface is called, and the name of the bean in the configuration file is set to the bean.

(7) If the Bean implements the Beanfactoryaware interface, the Setbeanfactory () method of the interface is called, and the Beanfactory container instance is set to the bean.

(8) If the beanfactory is assembled with a beanpostprocessor post processor, the Beanpostprocessor interface's object will be called The Postprocessbeforeinitialization method is used to process the bean. Where the incoming bean is the bean currently being processed, and Beanname is the configuration name of the current bean in the configuration file, returning the processed bean. Users can use this method to perform special processing on certain beans. The functions of AOP, dynamic proxy, etc. provided by the spring container are implemented through Beanpostprocessor.

(9) If the bean implements the Initializingbean interface, the Afterpropertiesset () method of the interface is called.

(10) This method is executed if the initialization method is specified through the Init-method property in the bean tag of the configuration file.

(11) If the Beanfactory is equipped with a Beanpostprocessor post processor, then the object Postprocessafterinitialization () method is executed here to process the bean.

The Beanpostprocessor processor defines two methods, namely Postprocessbeforeinitialization () and postprocessafterinitialization (), Called in step 8th and here respectively.

(12) If the Bean's scope= "prototype" is specified in the configuration file, which means that the bean configured is a multi-instance, each fetch of the bean will return a newly instantiated bean, so after this step the spring container no longer manages the bean of many cases, Returns the currently generated instance to the user directly.

For scope= "singleton" beans (by default), which means that the bean is singleton, it needs to be cached in the spring IOC container, which is fetched from the container every time the user gets it. And spring will perform subsequent life-cycle management of these beans.

(13) for singleton beans, when the container is closed, spring will be triggered to manage the subsequent life cycle of the bean. If the bean implements the Disposablebean interface, it calls the Destroy () method of the interface, where it can release resources, log logs, and so on.

(14) For a singleton bean, if the Destroy-method attribute is specified in the configuration file, spring executes the method of this property configuration and completes the operation of the bean resource release.

the above-mentioned life cycle methods can be broadly divided into the following four categories:

A, Bean's own method

Includes how the bean is constructed when the bean is instantiated, the set method of the bean when setting the property value, and the method specified by the Init-method and Destroy-method configured through the configuration file.

B, Bean-level life-cycle interface method

including Beannameaware, Beanfactoryaware, Initializingbean, and Disposablebean, these interface methods are implemented directly by the bean.

C, container-level life-cycle interface method

Includes Instantiationawarebeanpostprocessor and beanpostprocessor two interfaces. Generally called their implementation class as a post-processor, the post-processor interface is generally not implemented by the bean itself, they are independent of the bean, the implementation class is registered in the form of a container attachment to the spring container, and through the interface reflected to the spring container scan recognition. When the spring container creates any beans, these post-processors will work, and the impact of these post-processors is global.

D, factory post-processor interface method

including Aspectjweavingenabler, Customautowireconfigurer, Configurationclasspostprocessor and other classes, The Beanfactorypostprocessor interface is implemented. The factory post processor is also container-level and is called immediately after the context is applied to the assembly configuration file.

The spring container can also register multiple post processors, as long as they all implement the ordered interface, and the spring container calls these post processors sequentially in a specific order. the life cycle of beans in ApplicationContext 1. Life cycle Diagram

2, the specific process

The process is similar to the bean's life cycle in the above beanfactory and is no longer explained here. the difference between beanfactory and ApplicationContext

View the inheritance diagram for ApplicationContext in idea:

As you can see, ApplicationContext inherits Beanfactory,beanfactory as the more primitive factory in spring, which does not support the spring plugins such as AOP, web, etc. ApplicationContext not only includes all the features of the beanfactory, but also supports a variety of spring plug-ins, works in a framework-oriented way, and hierarchies and implements inheritance for contexts.

Beanfactory is the infrastructure of the spring framework for spring itself, while applicationcontext for developers using spring, compared to Beanfactory offers more features for practical applications, ApplicationContext can be used directly on almost all occasions rather than the underlying beanfactory.

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.