A good memory is better than a bad pen 86-spring3 learn (7) The life cycle of a bean in-applicationcontext

Source: Internet
Author: User

Suppose you use ApplicationContext to build and manage beans, and a bean from build to destroy will run through several stages.
I personally understand that the life cycle of a typical bean consists of: build, initialize, use phase, destroy four core phases. Using ApplicationContext and Beanfactory to the bean's life cycle, the macro is basically the same, there are some differences on the micro.

Suppose you use ApplicationContext to build and manage beans. In the Run Beanfactoryaware
After the Setbeanfactory () stage. If there is an implementation Org.springframework.context.ApplicationContextAware interface on the Bean class, run the Setapplicationcontext () method, Then proceed to the Beanpostprocessors processbeforeinitialization () and subsequent processes.

In a non-web application. To invoke the Destroy method set by the singleton Bean in the bean definition file before closing the container . You can run the Abstractapplicationcontext Registershutdownhook () method and register the relevant method with the JVM. Like what:
Abstractapplicationcontext context = new Classpathxmlapplicationcontext
("Beans-config.xml");
Context.registershutdownhook ();
Running the application

The Destroy method set on the bean definition file will be called to run before the application finishes.
Assuming that the beanfactory is used, only when the bean is actually obtained using the Getbean () method will it instantiate the action ;
Assuming that ApplicationContext is used, all beans are instantiated in advance against the contents of the bean definition file. Assuming this is not what you want, the ability to set the "Lazy-init" property to "true" onthe ApplicationContext does not instantiate the bean at startup. Like what:

<bean id="some" class="org.bearfly.bean.Some lazy-init="true"></bean>

A good memory is better than a bad pen 86-spring3 learn (7) The life cycle of a bean in-applicationcontext

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.