Detailed explanation of bean life cycle used by spring configuration
Basic concepts
The life cycle of a bean in spring refers to the process from creation to destruction of a bean.
Let's explore the next few properties about bean lifecycle configuration.
lazy-init
Lazy init means to delay loading beans. By default, all beans defined in the configuration file will be instantiated when the spring IOC container is initialized. If lazy init is enabled, beans will be created when calling beans.
Define bean:
The configuration method is as follows (take XML file as an example):
Call validation:
depends-on
Dependencies on refers to the order in which beans are initialized and destroyed. This property can be used to identify that one or more beans are explicitly forced to be initialized before the current bean is initialized. If the scope of the specified bean is singleton, it means that the bean specified by this attribute should be destroyed before the current bean is destroyed.
Define in bean:
Define in the configuration file:
Call validation:
Observe the output results. When calling beanone, spring will automatically create a beantwo instance.
init-method & destory-method
When instantiating a bean, you may need to perform an initialization operation to ensure that the bean is available. Similarly, when you remove a bean from the container when it is not needed, you may also need to do some clarity in order.
To define initialization and destruction operations for beans, you need to use the init method and destruction method attributes.
Define bean
Configure in XML file
Instantiate the bean in the IOC container and destroy it
summary
The above is all about the detailed explanation of the bean life cycle used in spring configuration in this article. I hope it will be helpful to you. Interested friends can continue to refer to this site: Inheritance and abstract code examples of beans in spring, factorybean code examples in spring, etc. if you have any questions, you are welcome to leave messages for exchange and discussion. Thank you for your support for the programming tips website!