- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 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/
我尝试阅读有关 Spring BOM、Spring Boot 和 Spring IO 的文档。 但是没有说明,我们应该如何一起使用它们? 在我的项目中,我们已经有了自己的 Parent POM ,所以
我正在开发的很酷的企业应用程序正在转向 Spring。这对所有团队来说都是非常酷和令人兴奋的练习,但也是一个巨大的压力源。我们所做的是逐渐将遗留组件移至 Spring 上下文。现在我们有一个 huuu
我正在尝试使用 @Scheduled 运行 Spring 批处理作业注释如下: @Scheduled(cron = "* * * * * ?") public void launchMessageDi
我对这两个概念有点困惑。阅读 Spring 文档,我发现,例如。 bean 工厂是 Spring 容器。我还读到“ApplicationContext 是 BeanFactory 的完整超集”。但两者
我们有一个使用 Spring BlazeDS 集成的应用程序。到目前为止,我们一直在使用 Spring 和 Flex,它运行良好。我们现在还需要添加一些 Spring MVC Controller 。
假设我有一个类(class) Person带属性name和 age ,它可以像这样用 Spring 配置: 我想要一个自定义的 Spring 模式元素,这很容易做到,允许我在我的 Sp
如何在 Java 中以编程方式使用 Spring Data 创建 MongoDB 复合索引? 使用 MongoTemplate 我可以创建一个这样的索引:mongoTemplate.indexOps(
我想使用 spring-complex-task 执行我的应用程序,并且我已经构建了复杂的 spring-batch Flow Jobs,它执行得非常好。 你能解释一下spring批处理流作业与spr
我实现了 spring-boot 应用程序,现在我想将它用作非 spring 应用程序的库。 如何初始化 lib 类,以便 Autowiring 的依赖项按预期工作?显然,如果我使用“new”创建类实
我刚开始学习 spring cloud security,我有一个基本问题。它与 Spring Security 有何不同?我们是否需要在 spring boot 上构建我们的应用程序才能使用 spr
有很多人建议我使用 Spring Boot 而不是 Spring 来开发 REST Web 服务。我想知道这两者到底有什么区别? 最佳答案 总之 Spring Boot 减少了编写大量配置和样板代码的
您能向我解释一下如何使用 Spring 正确构建 Web 应用程序吗?我知道 Spring 框架的最新版本是 4.0.0.RELEASE,但是 Spring Security 的最新版本是 3.2.0
我如何才能知道作为 Spring Boot 应用程序的一部分加载的所有 bean 的名称?我想在 main 方法中有一些代码来打印服务器启动后加载的 bean 的详细信息。 最佳答案 如spring-
我有一个使用 Spring 3.1 构建的 RESTful API,也使用 Spring Security。我有一个 Web 应用程序,也是一个 Spring 3.1 MVC 应用程序。我计划让移动客
升级到 Spring 5 后,我在 Spring Rabbit 和 Spring AMQP 中遇到错误。 两者现在都设置为 1.5.6.RELEASE 有谁知道哪些版本应该与 Spring 5 兼容?
我现在已经使用 Spring Framework 3.0.5 和 Spring Security 3.0.5 多次了。我知道Spring框架使用DI和AOP。我还知道 Spring Security
我收到错误 Unable to Location NamespaceHandler when using context:annotation-config running (java -jar) 由
在 Spring 应用程序中嵌入唯一版本号的策略是什么? 我有一个使用 Spring Boot 和 Spring Web 的应用程序。 它已经足够成熟,我想对其进行版本控制并在运行时看到它显示在屏幕上
我正在使用 spring data jpa 进行持久化。如果存在多个具有相同名称的实体,是否有一种方法可以将一个实体标记为默认值。类似@Primary注解的东西用来解决多个bean的依赖问题 @Ent
我阅读了 Spring 框架的 DAOSupport 类。但是我无法理解这些 DAOSuport 类的优点。在 DAOSupport 类中,我们调用 getXXXTemplate() 方法来获取特定的
我是一名优秀的程序员,十分优秀!