A young man who has worked for 3 years went to an interview and was asked questions about Spring.
The question was relatively simple, but he did not answer it.
Although he can find the answer through a search engine, if he doesn't understand, he won't be in the next interview!
This interview question is: "Bean in Spring, what are the scopes?"
For this question, look at the answers of ordinary people and experts.
Ordinary people:
Ok. . . . . . .
Expert:
OK, this question can be answered in several ways.
First of all, the IOC container in the Spring framework can easily help us manage Bean object instances in the application.
We only need to tell the IOC container which beans need to be managed by the IOC container according to the xml or annotations provided in Spring.
Secondly, since it is the management of Bean object instances, it means that these instances have a life cycle, which is the so-called scope.
In theory, there are only two general life cycles:
- A singleton, also known as a singleton, means that only one instance of a bean exists in the entire Spring container.
- Prototype, translated into prototype, means that every time the specified bean is obtained from the IOC container, a new instance object will be returned.
However, in the Web application based on the Spring framework, a session dimension is added to control the life cycle of the Bean. There are three main options.
- request, for each http request, a new bean will be created
- Session, taking the session session as the latitude, the same session shares the same Bean instance, and different sessions generate different Bean instances
- globalSession, for the global session latitude, share the same Bean instance
The above is my understanding of the problem.
Summarize
"The essence of the technical framework is to solve specific problems, so if you can think about Spring from the perspective of technology"
When encountering this kind of problem, you can infer the answer based on the scene, just like the master's answer.
Just like we write CRUD code now, it has become a basic ability to allow us to complete the development of complex business logic.
This issue of ordinary people VS master interview series ends here.
If you have any technical interview questions that you don't understand, please feel free to private message me at any time.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。