Explain in detail the definition and life cycle of beans in Java’s spring framework
The definition of beans forms the backbone of the application. The objects managed by the spring IOC container are called beans. Beans are instantiated, assembled, and managed through the spring IOC container. These beans are provided by the container. For example, in the < bean / > definition of XML, we have seen the formal configuration metadata creation in the previous chapters.
The bean definition contains the required container. You should know the following information called configuration metadata:
All of the above configuration metadata are converted into a set of the following properties to form the definition of each bean.
Spring configuration metadata the spring IOC container is completely decoupled by the format in which the configuration metadata is actually written. There are three important ways to configure the spring container of metadata provided below:
We have seen how XML based configuration metadata is provided to the container, but let us see different bean definitions, including delayed initialization, initialization method and destruction method. Another example of XML based configuration file:
The life cycle of a spring bean is easy to understand. When a bean is instantiated, it may need to perform some initialization to convert it to a usable state. Similarly, when the bean is no longer needed and is removed from the container, some cleanup may also need to be done.
However, there are also activities that take place in the scenario between the instantiation and destruction time behind the bean, but this chapter will only discuss two of them, which are the life cycle callback methods of important beans during bean initialization and destruction.
To define the installation and disassembly of a bean, we only declare the initialization method and / or destruction, and the parameters of the method are < bean >. Specify a method in the init method attribute, which is instantiated immediately after the called bean. Similarly, the destroy method specifies the method to be called before the bean is removed from the container.
Initialization callback: org springframework. beans. factory. The initializingbean interface specifies a single method:
void afterPropertiesSet() throws Exception; Therefore, you can simply implement the above interface and initialization work. You can use the afterpropertieset() method in it, as shown below:
In the case of XML based configuration metadata, you can use the init method attribute to specify the name of the method with void parameterless signature. For example:
The following is the definition of the class:
Destroy callback org springframework. beans. factory. The disposablebean interface specifies a single method:
Therefore, you can simply implement the above interface and finalize the work. You can do the destroy () method inside, as shown below:
In the case of XML based configuration metadata, you can use the destroy method attribute to specify the name of the method with void parameterless signature. For example:
The following is the definition of the class:
If you use spring's IOC container in a non web application environment, for example, in a desktop rich client environment; Register the close hook in the JVM. This ensures a normal shutdown and allows all resources to be released, calling the relevant destroy method on the singleton bean.
It is recommended not to use the initializingbean or disposablebean callback, because XML configuration provides great flexibility in naming your methods.
For example, use the eclipse IDE, and then follow the steps below to create a spring application: