当前位置:网站首页>Scope and lifecycle of beans
Scope and lifecycle of beans
2022-07-19 08:55:00 【A cool witch_】
Bean Scope and life cycle of
One 、 What is? Bean Scope of action
Bean The scope of can be understood as Bean stay Spring Something in the whole framework Patterns of behavior , It can also be understood as Bean The type of .
Two 、Bean Of 5 Species scope
- singleton: Single case scope
- prototype: Prototype scope ( Multiple scope )
- request: Request scope
- session: Conversation scope
- application: Global scope
| Scope | describe |
|---|---|
| singleton | stay Spring There is only one... In the container Bean example ,Bean In the form of a single example .Bean stay Spring The default is singleton |
| prototype | Every time you call getbean() when , All return a new instance |
| request | Every time HTTP The request will create a new one Bean |
| session | The same HTTP Session Share a Bean, Different HTTP Session Use different Bean |
| application | Limit one Bean Is the scope of ServletContext Life cycle of |
request session application These three scopes are based on web Of Spring WebApplicationContext Realized , Only in web It can only be used in the environment .
2.1 Singleton
If Bean The scope of is declared as Singleton, So in Spring Only one shared... Will be created in the container bean example . For all bean request , as long as id With this bean The definition matches , that Spring The same... Will be returned every time you need it bean example .
Singleton It's a singleton model , When you create a container, you create a bean object ,Singleton yes Spring Default scope in , Sure scope=(" ")" To set the scope .
Use scenarios : Usually No state Of Bean Use this scope . Stateless means Bean The property state of the object does not need to be updated .
2.2 prototype
When a scope is prototype when , It means a Bean The definition corresponds to multiple object instances . Declare as prototype The scope of will result in every time the bean A new... Is created when requested bean example .
prototype Is a prototype type ,** It was not instantiated when we created the container , But when we get bean Only when you create an object .** The object obtained each time is not the same object .
Use scenarios : It is usually stateful Bean Use this scope .
2.3 Request
When HTTP The scope of the request call is request Of bean when , Every time you add one HTTP request ,Spring I'm going to create a new one bean, Destroy this in time after the request is processed bean.
2.4 session
session All requests in share the same request bean example .Session Destroy when finished bean.
2.5 application
Servlet The container is the whole Web Application development uses appPreferences bean Define once to create AppPreferences bean example .
application And singleton There are some differences :
- It's every ServletContext Single case , Not every one of them Spring Application Single case .
- It's public , Therefore, it can be seen as a ServletContext attribute .
3、 ... and 、Bean Life cycle of
Life cycle refers to the whole life cycle of an object from birth to destruction .
String Bean The life cycle of can be divided into the following five parts :
- Instantiation
- Attribute assignment
- initialization
- Use
- The destruction


- Instantiation : First step , Instantiate a bean object .
- Attribute assignment : The second step , by bean Set related properties and dependencies
- initialization :3,4 Step is executed before initialization ,5,6 The first step is to really initialize , The first 7 Step is executed after initialization , After initialization , You can start using it bean 了
- The destruction :9,10 Step real destruction bean
We use a specific chestnut to explain this process :
PersonBean My life :
边栏推荐
- MySQL索引(一)
- Shell notes
- 小说里的编程 【连载之十四】元宇宙里月亮弯弯
- LeetCode 0115. Different subsequences
- QR分解求矩阵逆--c工程实现
- android 数据库左连接查询
- Distributed transaction best effort notification scheme
- ES6 learning function (strict mode, higher-order function, closure)
- How to set preferences when developing esp8266 and esp32 with Arduino
- 树状数组
猜你喜欢

C# - this 的用法

朋友圈如何定位?3个核心步骤,打造卖爆产品的朋友圈

Jsp+servlet+mysql案例

数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比

分布式事务-可靠消息最终一致性解决方案

idea按条件断点调试

解决ApplicationEventMulticaster not initialized - call ‘refresh‘ before multicasting events异常

Dynamic memory management
![[Hongke] Introduction to genicam protocol](/img/a5/91a1d24a47ea2e3265530778d6fc1f.png)
[Hongke] Introduction to genicam protocol

Graphite thickness measurement
随机推荐
2022年P气瓶充装考试练习题模拟考试平台操作
Collation of RPA related knowledge points
小说里的编程 【连载之十三】元宇宙里月亮弯弯
MySQL中查询一个字符串字段的值不为空到底该怎么写?
WPF 3D application building (Foundation)
Zero basic C language
2022t elevator repair examination question bank and answers
Programming in the novel [serial 12] the moon bends in the yuan universe
Left connection query of Android database
MySQL读写分离
mysql 初始化修改密码问题
Cocos shader basics 7
cut,sort,uniq,xargs
【无标题】
用于语义分割的Hausdorff损失函数
深度学习第四周Key Concepts on Deep Neural Networks习题整理
How to write a string field in MySQL if its value is not empty?
Programming in the novel [serial 11] the moon bends in the yuan universe
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
单机项目进行微服务拆分