Detailed explanation of the life cycle of spring bean

Bean lifecycle:

Bean definition - bean initialization - bean use - bean destruction

Definition of bean

A bean is a component model assembled by spring. All entity classes can be configured as a bean, which can then be used in any other bean. A bean can also not be a specified entity class, which is an abstract bean.

Initialization of bean

There are two ways to initialize callbacks for beans in spring

One is to declare init method = "init" in the configuration file, and then initialize with init () method in an entity class

The other is to implement the initializingbean interface and override the afterpropertieset () method.

First:

Profile:

Beaninitdemo1 class:

Test class:

Operation results:

Here is the init () method initialization setting

Reason: the init () initialization method is called after the bean initialization of the configuration file, so the assignment of message in the configuration file is changed.

Second:

Profile:

Write the beaninitialdemo2 class to implement the initializingbean interface

Test:

Running result: This overrides the afterpropertieset() method setting of the initializingbean interface

For the same reason, the afterpropertieset () method is executed after the bean of the configuration file is initialized, so the assignment of message in the configuration file is changed

Use of beans

There are two ways to use beans in spring:

1, beanfactory:

Beanfactory is delayed loading. If a bean property is not injected, beanfactory will not throw an exception until the getBean method is used for the first time after loading, that is, when the beanfactory is used to instantiate the object, the configured bean will not be instantiated immediately. When you use this bean, it will be instantiated (getBean).

2, ApplicationContext:

If ApplicationContext is used, the configured bean will be instantiated if it is a singleton, whether you use it or not. ApplicationContext checks when initializing itself, which is helpful to check whether the dependent attributes are injected. ApplicationContext is a subclass of beanfactory. In addition to all the functions of beanfactory, it also provides more complete framework functions, such as internationalization, resource access, etc. Therefore, we usually choose to use ApplicationContext.

Bean destruction

Like initialization, bean destruction provides two methods

First, declare destroy method = "cleanup" in the configuration file, and then write a cleanup () method in the class

The second is to implement the disposablebean interface and override the destroy () method

First:

Profile:

Beandestorydemo1 class:

Test:

Operation results:

context. Registershutdownhook() is to register and close the hook for spring. Close @ r before the program exits_ 404_ 1199 @, if not

context. registerShutdownHook(); The cleanup () method will not be executed.

Second:

Profile:

Beandestorydemo2 class:

Test:

Operation results:

Spring can manage the life cycle of beans in singleton scope, so you can do some work before bean initialization and destruction to manage beans more flexibly.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>