Interview Spring's bean life cycle

When looking for a job, some people will be asked about the life cycle of Beans in Spring. In fact, it is to check whether they are familiar with Spring. The content of which is rarely used in work, so let's take a brief look.

    Before explaining, you can think about the life cycle of Servlet: instantiation, initial init, receiving request service, destroying destroy;

    Beans in the Spring context are also similar, as follows

    1. Instantiate a Bean - which is what we often call new;

    2. Configure the instantiated bean according to the Spring context - that is, IOC injection;

    3. If the Bean has implemented the BeanNameAware interface, the setBeanName(String) method implemented by it will be called, and the id value of the Bean in the Spring configuration file is passed here.

    4. If the Bean has implemented the BeanFactoryAware interface, it will call the setBeanFactory it implements (setBeanFactory(BeanFactory) passes the Spring factory itself (you can use this method to get other beans, just configure a common Bean in the Spring configuration file) can);

    5. If the Bean has implemented the ApplicationContextAware interface, it will call the setApplicationContext(ApplicationContext) method and pass in the Spring context (the same method can also implement the content of step 4, but it is better than 4, because ApplicationContext is a sub-interface of BeanFactory, there are more implementation methods);

    6. If the Bean is associated with the BeanPostProcessor interface, the postProcessBeforeInitialization(Object obj, String s) method will be called. The BeanPostProcessor is often used to change the content of the Bean, and since this method is called at the end of the Bean initialization, it can also be be applied to memory or caching technologies;

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

    8. If the Bean is associated with the BeanPostProcessor interface, the postProcessAfterInitialization(Object obj, String s) method will be called;

    Note: After the above work is completed, the bean can be applied. The bean is a Singleton, so in general, the bean we call the same id will be an instance with the same content address. Of course, it can also be configured in the Spring configuration file. Non-Singleton, we will not go into details here.

    9. When the Bean is no longer needed, it will go through the cleanup phase. If the Bean implements the DisposableBean interface, the destroy() method it implements will be called;

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

 

The above 10 steps can be used as templates for interviews or written tests. In addition, what we describe here is the life cycle of applying Spring context beans. If the Spring factory is the BeanFactory, it is OK to remove step 5 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767454&siteId=291194637