简体   繁体   English

我们是否应该将Spring单例用于DAO

[英]Should we use spring singletons for DAOs

In our web application, we are using Spring as dependency injection mechanism. 在我们的Web应用程序中,我们使用Spring作为依赖项注入机制。 We have multiple DAOs and Manager spring beans which has a default scope of singleton.We have made sure that all of these beans are stateless. 我们有多个DAO和Manager弹簧bean,它们的默认作用域为单例,并确保所有这些bean都是无状态的。 I have read at multiple places why singleton pattern is bad : What is so bad about singletons? 我已经在很多地方读到了为什么单例模式不好:单例有什么不好? .Forgive me if this is asked already but please answer if the above mentioned design comes under bad coding practice and also how improvements can be made to same. 。请原谅我是否已问过,但请回答上述设计是否受不良编码规范的影响,以及如何对其进行改进。

As you can see in the first response to the link you posted ( what is so bad about singleton ) three problems of singleton are : 正如您在对发布的链接的第一个响应中看到的( 单身人士的不幸之处,单身 人士的 三个问题是

  • Hide dependencies of the application in the code 在代码中隐藏应用程序的依赖项
  • Violation of single responsible principle 违反单一责任原则
  • Code strong coupled 代码强耦合

None of them is present if you use the singleton via spring dependency injection , because: 如果您通过spring依赖注入使用单例,则它们都不存在 ,因为:

  • Dependencies are stored in the configuration file, not in the code 依赖关系存储在配置文件中,而不是代码中
  • The single responsible principle is not broken because the construction of your dao is handled by the spring engine, not by your dao 唯一负责任的原则没有被破坏,因为您的刀模的构造是由弹簧引擎而不是您的刀模来处理的
  • Code is not strong coupled because the configuration can be changed without changing the code 代码耦合不强,因为可以在不更改代码的情况下更改配置

So yes using spring singletons can be a very nice solution not having problems of real singletons. 因此,是的,使用春季单身人士可能是一个很好的解决方案,而不会遇到真正的单身人士的问题。

Spring singleton beans are not using the singleton pattern. 春季单例豆不使用单例模式。 They just happen to be instantiated once by the framework, and injected into every component that needs them. 它们只是被框架实例化了一次,并注入到需要它们的每个组件中。

The singleton pattern is considered an anti-pattern mainly because it makes all the classes relying on it hard to test. 单例模式被认为是反模式,主要是因为它使所有依赖它的类都难以测试。 Spring singleton beans don't have this problem at all. 春季单例豆根本没有这个问题。

The primary disadvantage of singletons is that they tend to cause entanglements between different client classes if they have any state. 单例的主要缺点是,如果它们具有任何状态,它们往往会导致不同客户端类之间的纠缠。 Well-designed service classes should not have any operation-specific state (they'll usually have something like a reference to a database pool or other resources, injected at instantiation and then not changed after), and it's perfectly fine to use a singleton-scoped bean for such services. 设计良好的服务类不应具有任何特定于操作的状态(它们通常具有对数据库池或其他资源的引用,在实例化时注入,然后在之后不进​​行更改),并且使用单例-适用于此类服务的范围限定的bean。

If you read the answers from the question you've linked, they don't really apply to Spring singletons. 如果您从链接的问题中读到答案,它们实际上并不适用于Spring单例。 When using Spring beans in singleton scope they're not hard to test, they do not control their own creation, and they are accessed through an interface (or at least should be!). 当单范围使用Spring豆他们不是很难测试,他们控制自己的创造, 它们通过一个接口来访问(或至少是应该的!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ORMLite 中,当 Object 似乎可以工作时,为什么我们应该使用具有特定类型 ID 的 DAO? - In ORMLite why should we use DAOs with an ID of a specific type when Object seems to work? 什么时候我应该使用懒惰的单身人士而不是普通的单身人士? - When should I use lazy Singletons over normal Singletons? 我们应该密封单身人士吗?我们应该首先尝试从单身人士继承吗? - Should we seal Singletons? Should we try to inherit from Singletons in the first place? 我应该在单例中使用哪个android上下文? - Which android context should i use in singletons? 我们什么时候应该在Spring中使用@Component? - When should we use @Component in Spring? 我应该使用生产代码中的DAO来设置和验证测试吗? - Should I use DAOs from production code to setup & verify tests? 如何在不使用DAO的情况下使用Spring LDAP连接到多个URL? - How to use Spring LDAP to connect to multiple urls without DAOs? 为什么我们应该使用 Spring 数据 JPA 映射? - Why we should use Spring Data JPA mappings? 我们应该避免在不需要时使用spring managed bean吗? - should we avoid to use spring managed bean when it is unnecessary? Spring Java中许多DAO的策略 - Strategy for many DAOs in Spring Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM