bean是单例作用域

bean是单例作用域

本文介绍了为什么Spring bean是单例作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernet和Spring,它运行的很好..但是我有一些疑问

I am working with Hibernet and Spring it's going good..but I have some doubts

1)为什么默认情况下spring范围是单例的?是否有任何原因

1) why spring scope is singleton by default?Is there any reason for that

2)我可以在Hibernate实体中写final变量吗? 示例:

2) Can I write final varible in Hibernate entity? Example :

@Entity
public class Emp {
  @Id
  private Long id;
  final private String panNo;
}

我可以像上面一样写

3)静态变量可以Searlizable吗?

3) static varibles can Searlizable?

推荐答案

无状态bean规则:)如果您不打算将状态数据保存在bean中,则每个bean仅具有一个实例就足够了.您还应该记住,这不是JVM单例,而是Spring单例.因此,您不必仅提供私有构造函数和任何getInstance()方法.

Stateless beans rules :) If you're not going to hold state data in beans then it's enough to have only one instance of each bean. You should also remember that it's not JVM singletons - just Spring singletons. So you don't have to provide only private constructor and any getInstance() methods.

Spring文档中的语录:

Quote from Spring documentation:

仅当必须保留一些会话详细信息时,才应使用例如会话范围.

Only when you have to keep some session details you should use for example session scope.

这篇关于为什么Spring bean是单例作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:16