当前位置:网站首页>Bean的作用域、执行流程、生命周期
Bean的作用域、执行流程、生命周期
2022-07-28 05:16:00 【在上山的mei】
目录
1.2.1单例作⽤域(singleton)和全局作⽤域(application)区别
1.Bean作用域(很重要,面试会考)
1.1Bean作用域定义
Bean 的作⽤域是指 Bean 在 Spring 整个框架中的某种⾏为模式
1.2Bean 的 6 种作⽤域
1. singleton(是Bean的默认作用域)
该作⽤域下的Bean在IoC容器中只存在⼀个实例:获取Bean(即通过applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是同⼀个对象,那么当其他⼈修改了这个值之后,那么另⼀个⼈读取到的就是被修改的值
使用场景:通常⽆状态的Bean(Bean对象的属性状态不需要更新)使⽤该作⽤域
看一个其他⼈修改了这个值之后,那么另⼀个⼈读取到的就是被修改的值的案例:

2. prototype
每次对该作⽤域下的Bean的请求都会创建新的实例:获取Bean(即通applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是新的对象实例。
使用场景场景:通常有状态的Bean(Bean对象的属性状态需要更新)使⽤该作⽤域
举例:

3. request
每次http请求会创建新的Bean实例,类似于prototype
使用场景:⼀次http的请求和响应的共享Bean
4. session
在⼀个http session中,定义⼀个Bean实例
使用场景:⽤户回话的共享Bean, ⽐如:记录⼀个⽤户的登陆信息
5. application
在⼀个http servlet Context中,定义⼀个Bean实例
使用场景:Web应⽤的上下⽂信息,⽐如:记录⼀个应⽤的共享信息
6. websocket
在⼀个HTTP WebSocket的⽣命周期中,定义⼀个Bean实例
使用场景:WebSocket的每次会话中,保存了⼀个Map结构的头信息,将⽤来包裹客户端消息
头。第⼀次初始化后,直到WebSocket结束都是同⼀个Bean
1.2.1单例作⽤域(singleton)和全局作⽤域(application)区别
1.作用于的区别:singleton 是 Spring Core 的作⽤域,application 是 Spring Web 中的作⽤域
2.容器的区别:singleton 作⽤于 IoC 的容器,⽽ application 作⽤于 Servlet 容器。
2.Bean 执⾏流程
启动 Spring 容器 -> 实例化 Bean(分配内存空间,从⽆到有)-> Bean 注册到 Spring 中(存操作) -> 将 Bean 装配到需要的类中(取操作)
3.Bean ⽣命周期(分为5个部分)
⽣命周期指的是⼀个对象从诞⽣到销毁的整个⽣命过程

生命周期演示:
边栏推荐
- Look at the experience of n-year software testing summarized by people who came over the test
- PC side bug record
- Specific differences between typedef and define
- Dell remote control card uses ipmitools to set IPMI
- Redis configuration file explanation / parameter explanation and elimination strategy
- Data security is gradually implemented, and we must pay close attention to the source of leakage
- After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
- HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
- RT based_ Distributed wireless temperature monitoring system based on thread
- C language classic 100 question exercise (1~21)
猜你喜欢

RT_ Use of thread message queue

【ARXIV2205】Inception Transformer

Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam

The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies

基于MPLS构建虚拟专网的配置实验

Evolution of ape counseling technology: helping teaching and learning conceive future schools

MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?

Google browser cannot open localhost:3000. If you open localhost, you will jump to the test address

UI automation test farewell from now on, manual download browser driver, recommended collection

Dcgan:deep volume general adaptive networks -- paper analysis
随机推荐
How to successfully test php7.1 connecting to sqlserver2008r2
Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam
Improving the readability of UI layer test with puppeter
Transformer -- Analysis and application of attention model
FreeRTOS learning (I)
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
How to send and receive reports through outlook in FastReport VCL?
Making RPM packages with nfpm
list indices must be integers or slices, not tuple
Autoreleasepool problem summary
HDU 3592 World Exhibition (differential constraint)
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
UI automation test farewell from now on, manual download browser driver, recommended collection
Table image extraction based on traditional intersection method and Tesseract OCR
Barbie q! How to analyze the new game app?
HDU 1530 maximum clique
RT based_ Distributed wireless temperature monitoring system based on thread
POJ 3728 the merchant (online query + double LCA)
Applet import project
阿里怎么用DDD来拆分微服务?