修改了5.1.5小节bean作用域的描述#1
Closed
FatOnionLee wants to merge 1 commit intoSnailclimb:masterfrom
FatOnionLee:bean-scope
Closed
修改了5.1.5小节bean作用域的描述#1FatOnionLee wants to merge 1 commit intoSnailclimb:masterfrom FatOnionLee:bean-scope
FatOnionLee wants to merge 1 commit intoSnailclimb:masterfrom
FatOnionLee:bean-scope
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
我是一个即将参加校招的非科班生,今天读完了Guide哥的面试突击版,非常感谢Guide哥的总结,从这里也学到了很多。看到5.1.4小节的Spring 中的 bean 的作用域的时候看到有些描述不是很能理解,在网上查了一些资料以后,在这里提出,希望能有所帮助。
原文
5.1.5 Spring 中的 bean 的作用域有哪些?
这里的表述有两个地方不太好理解
1 prototype的作用范围
在《JavaGuide面试突击版》中,prototype的作用范围的描述是
每次请求都会创建一个新的 bean 实例。感觉原文的表述不太清楚,似乎是想说每次HTTP请求都会创建一个bean实例。(当然也可能是我理解有误会)在Spring5.2.5官方文档中的描述是
Scopes a single bean definition to any number of object instances.。官方文档的说法也不清楚。W3Cschool里的描述是
每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()。我觉得这个描述是比较清晰并且容易理解的。2 session的作用范围
原文描述是
每一次HTTP请求都会产生一个新的 bean,该bean仅在当前 HTTP session 内有效,这应该是不对的,session的作用范围是在同一个session中共享一个bean实例,不同的session中使用不同的bean实例。而在同一个session里可以发起多个http请求,所以原文的描述每一次HTTP请求都会产生一个新的 bean显然是不对的。修改建议
可以参考W3Cschool里的描述