当前位置:网站首页>3. Bean scope and life cycle
3. Bean scope and life cycle
2022-08-02 01:41:00 【You really can't stop eating small dried fish】
Table of Contents
Scope Definition
The scope of a Bean refers to a certain behavior pattern of the Bean in the entire Spring framework, such as the singleton singleton scope, which means that there is only one copy of the Bean in the entire Spring, and it is globally shared, so when other people modify itAfter reading this value, what another person reads is the modified value.
6 scopes of beans
When the Spring container initializes an instance of a bean, it also specifies the scope of the instance.Spring has 6 scopes, the last four are based on Spring MVC:
\1. singleton: singleton scope --> Spring selects this scope by default
\2. prototype: prototype scope (multiple instance scope)
instance.
\3. request: request scope (a request-response scope)
\4. session: session scope (a session scope)
\5. application: global scope (understand)
\6. websocket: HTTP WebSocket scope (understand)
Note: The last four states are values in Spring MVC, and there are only the first two in a normal Spring project.

Bean execution process
Bean execution process (Spring execution process): start the Spring container -> instantiate the bean (allocate memory space, from scratch) -> register the bean in Spring (storage operation) -> assemble the bean to the required classin (fetch operation).
Bean Lifecycle
The life cycle of a bean is divided into the following 5 parts:
1. Instantiate Bean (allocate memory space for Bean, similar to new object)
2. Setting properties (Bean injection and assembly)
3.Bean initialization
3.1 implements various Aware notification methods, such as the interface methods of BeanNameAware, BeanFactoryAware, and ApplicationContextAware;
3.2 Execute BeanPostProcessor initialization pre-method;
3.3.1 Execute the @PostConstruct initialization method, which is executed after the dependency injection operation;
3.3.2 Execute the init-method method specified by yourself (if specified);
3.4 Execute the BeanPostProcessor initialization post method.
4. Use Bean
5. Destroy the Bean (the container can only be destroyed when the container is closed)
Execution process

边栏推荐
猜你喜欢
随机推荐
kubernetes之服务发现
C语言实验七 二维数组程序设计
typescript34-class的基本使用
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
Kubernetes — Flannel
C语言实验十 函数(二)
华为5年女测试工程师离职:多么痛的领悟...
哈希表
Oracle data to mysql FlinkSQL CDC to achieve synchronization
mapbox使用教程
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
【图像融合】基于加权和金字塔实现图像融合附matlab代码
Anti-oversold and high concurrent deduction scheme for e-commerce inventory system
ofstream,ifstream,fstream读写文件
canal realizes mysql data synchronization
创新项目实战之智能跟随机器人原理与代码实现
C语言实验八 字符数组程序设计
Debian侵犯Rust商标,妥协改名还是会得到豁免?
一本适合职场新人的好书
Newton's theorem and related corollaries









