Spring bean life cycle, how to manage

1. Instantiate a Bean, which is what we usually call new

2. Configure the instantiated Bean according to the Spring context, that is, IOC injection

3. If this Bean implements the BeanNameAware interface, it will call the setBeanName(String beanId) method it implements, where the ID of the Bean in the Spring configuration file is passed

4. If this Bean implements BeanFactoryAware Interface, it will call the setBeanFactory() it implements, and pass the Spring factory itself (you can use this method to get other Beans)

5. If this Bean implements the ApplicationContextAware interface, it will call setApplicationContext(ApplicationContext) Method, passing in the Spring context, this method can also achieve step 4, but it is better than 4, thinking that ApplicationContext is a subinterface of BeanFactory, there are more implementation methods

6. If this Bean is associated with BeanPostProcessor The interface will call the postProcessBeforeInitialization(Object obj, String s) method. BeanPostProcessor is often used to change the content of the Bean, and because this calls the After method at the end of the Bean initialization, it can also be used for memory or caching technology.

7. If this Bean is configured with the init-method property in the Spring configuration file, its configured initialization method will be automatically invoked

8. If this Bean is associated with the BeanPostProcessor interface, postAfterInitialization(Object obj, String s) method

Note: This Bean can be used after the above work is completed, then this Bean is a single, so in general, we call the Bean with the same ID at the content address The same example

9. When the Bean is no longer needed, it will go through the cleaning phase. If the Bean implements the DisposableBean interface, the destroy method implemented by it will be called

10. Finally, if The destroy-method attribute is configured in the Spring configuration of this Bean, and its configured destroyer will be automatically called

< /p>

Leave a Comment

Your email address will not be published.