为什么 Python 没有 Spring DI(组件生命周期)框架?
这个问题的重点并不是Spring所熟知的DAO、MVC、Messaging等组件框架,而只是核心。
据我了解,Spring 早期的优势是它管理组件生命周期,提供一个 Singleton Factory,在启动时读取的配置文件中创建对象。
然后,单例工厂根据请求将所有服务 bean 创建为单例。这是 Spring 的一大胜利,因为它使用更少的内存,并且 JVM 中的垃圾收集也少得多。另一方面,数据对象仍然被创建和销毁,通常不是作为单例。
为什么依赖注入对于Python或者其他语言没有那么大的优势呢?或者它们被这样使用?
This question is not focused on the DAO, MVC, Messaging and other component frameworks that Spring is known for, but just the Core.
As I understand it, Spring's earlier advantage was that it managed the component lifecycle, providing a Singleton Factory that creates the objects in the configuration file read on startup.
The singleton factory then creates all the service beans upon request as singletons. This is one of the big wins with Spring, as it uses less memory and there's a lot less garbage collection in the JVM. Data objects on the other hand are still created and destroyed, often not as singletons.
Why doesn't dependency injection have such big advantages for python or other languages? Or are they being used as such?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我从来没有觉得有必要在 Python 中使用这样的东西——而且我认识的大多数 Python 程序员也没有寻找这样的东西。我的印象是,Python 对象总体上是轻量级的,创建大量对象并避免可变对象的固有问题比为了优化而使用单例更好。
无论如何,如果一个人确实想要某种单例,通常的方法是在模块中创建一个值:
然后使用它:
甚至可以,比方说,替换模块中的值,就像通过交换Spring的
applicationContext.xml
文件。它在测试中很有用,例如:实际上,您甚至可以模拟整个类:
总结:恕我直言,Python 程序员并不觉得像 Spring DI 这样的东西有必要。如果需要Spring DI的某些功能,通过纯Python代码很容易实现。
(根据我的经验,我相信更多人可以指出其他原因。)
Well, I have never felt the need to use such a thing in Python - and most Python programmers I know did not looked for something like this either. I have the impression that Python objects are lightweight in general and it would be better to create a lot of them and avoid the inherent problems of mutable objects than to use singletons for the sake of optimization.
Anyway, if one really wants some kind of singleton, the usual way is to create a value in a module:
Then you use it:
You can even, let us say, replace the value in the module, as you would do by swapping the
applicationContext.xml
files of Spring. It can be useful in tests, for example:Actually, you can mock even the whole class:
Summarizing: IMHO, Python programmers do not feel the necessity of something like Spring DI. If one needs some functionality of Spring DI, it is easy to implement through pure Python code.
(There are points based in my experience. I am sure more people can point another causes.)
Python 具有不同的相对操作成本。它不使用垃圾回收,而是使用引用计数,这意味着释放对象并不像 Java 中那样昂贵。在我看来,仅仅为了加速而使用单例并不是很好的设计,因为它使系统的不同部分之间产生了更多的相互依赖性。
另一方面,依赖注入的设计优势(更清晰的代码,更好的封装)也适用于Python,并且存在这样的框架:
Python 依赖注入框架
Python has a different relative cost of operations. Instead of garbage collection, it uses reference counting, which means that releasing objects is not as expensive as in Java. Using singletons just for the speedup is not very good design in my opinion, as it makes more interdependencies between different parts of the system.
On the other hand, the design benefits of dependency injection (clearer code, better encapsulation) apply also to Python, and there exists such frameworks:
Python Dependency Injection Framework