java - Spring in Practice 书第 1 章中关于单例 bean 不维护状态所需的揭秘

标签 java spring spring-mvc

我正在学习 Spring,并且发现 Spring in Practice 这本书是一个很棒的资源。但是我不完全理解 Spring bean 单例范围。我在 bean 作用域部分读到的内容让我有些困惑。

这是该部分的摘录:

As you request singleton beans from the container, Spring will create instances and cache them if they haven’t been created; otherwise, Spring will return already-existing instances from its cache. Therefore, singleton beans in Spring often don’t maintain state because they’re usually shared among multiple threads (such as in servlet environments). For example, singleton services often have references to singleton DAOs, and the DAOs might have references to Hibernate SessionFactorys, which are threadsafe.

现在我对 Java 中的多线程了解不多,请原谅我的无知。在上面的摘录中说“因此,Spring 中的单例 bean 通常不维护状态,因为它们通常在多个线程之间共享”

1).这对域对象意味着什么?我可以将一个名为 User 的域对象定义为 spring 上下文中的单例范围吗?会发生什么?

2).我注意到大多数数据源和大型配置通常都在 spring xml 文件中。我有一种感觉,我的有关默认为单例的状态和 bean 的问题与仅连接应用程序的大部分有关。

有人可以为我澄清这个话题吗?一个例子会很有帮助。

提前谢谢你。

最佳答案

Java 类是对状态和行为的描述。该类的实例是可以展示行为的状态的持有者。因此,这个类

public class Foo {
    private String bar;
    public Foo(String bar) {this.bar = bar;}
    public void print() {System.out.println(bar);}
    public void setBar(String bar) {this.bar = bar;}
}

描述了一个数据结构,它有一个 String 作为状态,print()setBar(String) 方法作为行为。如果你创建一个实例

new Foo("the state");

您现在有了一个实际的数据结构,它包含 String 数据(或状态)"the state" 并且可以通过调用 print 来显示该数据() 方法或使用 setBar(String) 更改它。

上面类的问题在于它是可变的,即。它的状态可以改变。如果有多个线程对其进行操作,您可能会看到意外行为。例如,一个线程在另一个线程有机会 print() 输出其先前值之前更改了 bar 的值。

What does this mean for domain objects? Can I have a domain object called User defined as a singleton scope in the spring context? What would happen?

这对于域对象的意义与它对于服务或表示对象的意义完全相同。您可以在 Spring 上下文中将 User 类型的域对象定义为单例范围。如果你想是一个不同的问题。您是否希望您的应用程序只能访问一个 User 实例?不,那就不要让它成为单例。是的,然后将其设为单例。这仅适用于 Spring 实际为您管理实例的情况。

更多说明:

了解 Singleton 是什么很重要是。维基百科说

restricts the Instantiation of a class to one object

如果我们对 JVM 进行限制,则可以按照描述的方式实现单例 here .如果我们谈论的是关于 Spring ApplicationContext 的限制,那么我们谈论的是 Spring 管理的 singleton scope。 bean(仔细阅读本章)。例如

<bean id="customBean" class="org.mine.CustomBean" />

像这样检索

(CustomBean) context.getBean("customBean");
// or
context.getBean(CustomBean.class);

如果我们总是从 Spring 的 ApplicationContext 中检索实例,那么实际上我们只会实例化该类一次。

So if that variable is being accessed by different parts of the application, the state of the object can be changed by that variable by executing for instance the setter methods on that variable. But that seems like a singleton to me.

Web 应用程序是多线程的。如果每个线程都访问单例 bean 并改变它,您可能会得到 lost updates .这些通常是不需要的,因为您无法预测它们,因此很难测试。

That variable can be used to keep changing the same data structure throughout different parts of the application. So how is the Foo object not a singleton?

如前所述,Singleton 是一种模式,它描述了实例在某些上下文中的独特性。简单的做

new Foo("the state");

不会使该对象成为单例。正是您使用它的方式使它成为单例。

关于java - Spring in Practice 书第 1 章中关于单例 bean 不维护状态所需的揭秘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341358/

相关文章:

java - 在 mongodb 聚合中查找

java - Apache Common中的XMLConfiguration是否支持CDATA?

Java 保存文件错误

java - Android ProgressDialog 未显示(被其他线程中的代码阻止)

java - 如何验证@PathVariable是否已设置?

java - 从 Spring REST API 返回 HTTP 代码 200

Java Play 框架全局变量

java - Spring HandlerInterceptors 是如何实例化的?

java - Spring CrudRepository 不工作保存和删除操作

java - spring mvc @ExceptionHandler 方法获得相同的 View