Spring Bean Life Cycle Detailed

Source: Internet
Author: User

Object life cycle: Create (instantiate----initialize)---Use----destroy, and in spring, the Bean object cycle follows this process, but spring provides many external interfaces that allow developers to do things before and after three processes (instantiation, initialization, destruction). In spring beans, instantiation is the creation of space (constructors) for Bean objects, initialization is the initialization of attributes, and attribute injection (setter method injection properties).

1.Bean Self Method: Init-method/destroy-method, specify the appropriate execution method by adding the corresponding attribute to the profile bean definition.

2.Bean Life cycle interfaces: Beannameaware, Beanfactoryaware, Initializingbean, and Diposablebean methods for these interfaces. Each bean is selected for implementation, and individual actions can be selected.

3. Container-level life-Cycle interface method: This includes both the Instantiationawarebeanpostprocessor and Beanpostprocessor interface implementations (the former inherits from the latter), which is generally called the "post processor" of their implementation class, These interfaces are called every time the bean is instantiated or initialized.

4. Factory post-processor interface methods: They are also container-level, but they are called after the context appliance configuration file (Beanfactorypostprocessor, Customautowireconfigurer).


Bean Specific life cycle

1.postProcessBeanFactory (configurablelistablebeanfactory C): Factory post processor, this incident IOC container (ApplicationContext) is initialized as part of the. Implementation: Write a class, implement the Beanfactorypostprocessor interface, override the method, and

A bean for the class is configured in the spring configuration file.

2.postProcessBeforeInstantiation (class<? >c,string beanname): All Bean objects (note 1) are instantiated before execution, specifically before each Bean class constructor is executed. Implementation: Write a class that implements the Instantiationawarebeanpostprocessor connection

Port, override this method to configure a bean for the class in the spring configuration file. Returns an object, but you can actually return a null.

3.Bean instantiation, invoking the Bean class constructor.

4.postProcessAfterInstantiation (Object bean,string beanname): After the Bean class is instantiated, it is called before initialization.

5.postProcessPropertyValue: Called before property injection.

6.setBeanName (String beanname): Called after the property is injected to let the Bean class know the name or id attribute of the bean in which it resides.

7.setBeanFactory (Beanfactory Factory): Called after Setbeanname, the method is to let the Bean class know the properties of their beanfactory.

8.postProcessBeforeInitialization (Object bean,string beanname): Beanpostprocessor The role of the bean instantiation, initialization to do some preprocessing operations.

9.postProcessBeforeInitialization (Object bean,strign beanname)

10.afterPropertiesSet (): The Bean class implements the Initializingbean interface, overriding the method. Initialization works, but implementing the interface with this method and spring coupling is not recommended.

11.postProcessAfterInitialization (Object bean,strign beanname)

12.postProcessAfterInitialization (Object bean,strign beanname)

13. Program execution, Bean work

14.destroy (): Executes before the bean is destroyed.

15.xml_destroy (): Implement: Configure Bean Properties Destroy-method= "Xml_destroy" in Spring Bean configuration file, this method name is defined by the developer.

Spring Bean Life Cycle Detailed

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.