【问题标题】:Spring ApplicationContext Bean ScopeSpring ApplicationContext Bean作用域
【发布时间】:2009-07-30 23:07:34
【问题描述】:

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

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

【问题讨论】:

    标签: java spring


    【解决方案1】:

    默认情况下,在 Spring 中创建的 bean 的范围是 singleton,所以是的,在这些情况下,每个人都将访问同一个实例。另一种方法是将范围指定为 prototype

    更多信息请点击此处,第 3.4.1 和 3.4.2 节:

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

    【讨论】:

    • 哇!它就在文档中。我不知道我是怎么错过的,但非常感谢你指点我。
    【解决方案2】:

    默认情况下,在 Spring 中创建的 bean 是单例的。但是,如果您使用 Spring DispatcherServlet 和 DispatcherPortlet,则需要一个 bean 范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2013-03-02
      • 2011-08-02
      • 2017-06-27
      • 2017-09-02
      • 2011-10-14
      • 1970-01-01
      相关资源
      最近更新 更多