- Spring 5 Design Patterns
- Dinesh Rajput
- 470字
- 2021-07-08 09:59:28
Life of a bean in the container
The Spring application context uses the Factory method design pattern to create Spring beans in the container in the correct order according to the given configuration. So the Spring container has the responsibility of managing the life cycle of the bean from creation to destruction. In the normal java application, Java's new keyword is used to instantiate the bean, and it's ready to use. Once the bean is no longer in use, it's eligible for garbage collection. But in the Spring container, the life cycle of the bean is more elaborate. The following image shows the life cycle of a typical Spring bean:

The life cycle of a Spring bean in the Spring container is as follows:
- Load all bean definitions, creating an ordered graph.
- Instantiate and run BeanFactoryPostProcessors (you can update bean definitions here).
- Instantiate each bean.
- Spring injects the values and bean references into the beans' properties.
- Spring passes the ID of the bean to the setBeanName() method of the BeanNameAware interface if any bean implements it.
- Spring passes the reference of the bean factory itself to the setBeanFactory() method of BeanFactoryAware if any bean implements it.
- Spring passes the reference of the application context itself to the setApplicationContext() method of ApplicationContextAware if any bean implements it.
- BeanPostProcessor is an interface, and Spring allows you to implement it with your bean, and modifies the instance of the bean before the initializer is invoked in the Spring bean container by calling its postProcessBeforeInitialization().
- If your bean implements the InitializingBean interface, Spring calls its afterPropertiesSet() method to initialize any process or loading resource for your application. It's dependent on your specified initialization method. There are other methods to achieve this step, for example, you can use the init-method of the <bean> tag, the initMethod attribute of the @Bean annotation, and JSR 250's @PostConstruct annotation.
- BeanPostProcessor is an interface, and spring allows you to implement it with your bean. It modifies the instance of the bean after the initializer is invoked in the spring bean container by calling its postProcessAfterInitialization().
- Now your bean is ready to use in the step, and your application can access this bean by using the getBean() method of the application context. Your beans remain live in the application context until it is closed by calling the close() method of the application context.
- If your bean implements the DisposibleBean interface, Spring calls its destroy() method to destroy any process or clean up the resources of your application. There are other methods to achieve this step-for example, you can use the destroy-method of the <bean> tag, the destroyMethod attribute of the @Bean annotation, and JSR 250's @PreDestroy annotation.
- These steps show the life cycle of Spring beans in the container.
- The next section describes the modules that are provided by the Spring Framework.
推薦閱讀
- Spring Cloud Alibaba核心技術與實戰(zhàn)案例
- Boost C++ Application Development Cookbook(Second Edition)
- Mastering phpMyAdmin 3.4 for Effective MySQL Management
- Learning Elixir
- Visual C
- Oracle從入門到精通(第5版)
- UML 基礎與 Rose 建模案例(第3版)
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- 現(xiàn)代C++編程實戰(zhàn):132個核心技巧示例(原書第2版)
- INSTANT Adobe Edge Inspect Starter
- Developing SSRS Reports for Dynamics AX
- Getting Started with Python and Raspberry Pi
- jQuery從入門到精通(微課精編版)
- Learning C++ by Creating Games with UE4
- Java核心技術速學版(第3版)