java Spring ApplicationContext Bean 作用域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1209874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:38:51  来源:igfitidea点击:

Spring ApplicationContext Bean Scope

javaspring

提问by Buns of Aluminum

When you create a Service bean or Dao bean in your Spring applicationContext.xml file, what is the scope of those beans?

当您在 Spring applicationContext.xml 文件中创建 Service bean 或 Dao bean 时,这些 bean 的作用域是什么?

Will every person who accesses the web application use the same instance of the bean, or is the bean instantiated for each user's session?

每个访问 Web 应用程序的人都使用相同的 bean 实例,还是为每个用户的会话实例化 bean?

回答by Jon

By default a bean created in Spring is of scope singleton, so yes each person will access the same instance in those cases. The alternative is to specify the scope as prototype.

默认情况下,在 Spring 中创建的 bean 的作用域是singleton,因此是的,在这些情况下,每个人都将访问相同的实例。另一种方法是将范围指定为原型

More info on this here, sections 3.4.1 and 3.4.2:

此处的更多信息,第 3.4.1 和 3.4.2 节:

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes-prototype

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes-prototype