Spring for Python 与 Spring for Java 相比如何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/940564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:28:50  来源:igfitidea点击:

How does Spring for Python compare with Spring for Java

javapythonspring

提问by Sathya

I am an avid fan of the Spring framework for Java (by Rod Johnson). I am learning Python and was excited to find about Spring for Python. I would be interested in hearing the community's views on the comparison of these two flavours of Spring. How well does it fit Python's paradigms etc.

我是 Java 的 Spring 框架(由 Rod Johnson 编写)的狂热粉丝。我正在学习 Python,并且很高兴找到 Spring for Python。我很想听听社区对这两种 Spring 的比较的看法。它如何适合 Python 的范式等。

回答by Ants Aasma

Dependency injection frameworks are not nearly as useful in a dynamically typed language. See for example the presentation Dependency Injection: Vitally important or totally irrelevant?In Java the flexibility provided by a dependency injection framework is vital, while in Python it usually results in unneeded complexity.

依赖注入框架在动态类型语言中几乎没有那么有用。例如,请参阅演示文稿依赖注入:至关重要还是完全不相关?在 Java 中,依赖注入框架提供的灵活性至关重要,而在 Python 中,它通常会导致不必要的复杂性。

This doesn't mean that the principles are wrong. See this example how to achieve loose coupling between classes by using simple idioms:

这并不意味着这些原则是错误的。请参阅此示例如何通过使用简单的习语实现类之间的松散耦合:

# A concrete class implementing the greeting provider interface
class EnglishGreetingProvider(object):
    def get_greeting(self, who):
        return "Hello %s!" % who

# A class that takes a greeting provider factory as a parameter
class ConsoleGreeter(object):
    def __init__(self, who, provider=EnglishGreetingProvider):
        self.who = who
        self.provider = provider()
    def greet(self):
        print(self.provider.get_greeting(self.who))

# Default wiring
greeter = ConsoleGreeter(who="World")
greeter.greet()

# Alternative implementation
class FrenchGreetingProvider(object):
    def get_greeting(self, who):
        return "Bonjour %s!" % who
greeter = ConsoleGreeter(who="World", provider=FrenchGreetingProvider)
greeter.greet()

回答by gregturn

DISCLOSURE: I am the project lead for Spring Python, so you can consider my opinion biased.

披露:我是 Spring Python 的项目负责人,所以你可以认为我的观点有偏见。

I find that several of the options provided by Spring Python are useful including: aspect oriented programming, dependency injection, remoting, security, and easy database access.

我发现 Spring Python 提供的几个选项很有用,包括:面向方面的编程依赖注入、远程处理、安全性和轻松的数据库访问

Aspect oriented programming is, as they say, easier to implement off the cuff with python than java. But Spring Python makes it easy enough to add to existing python modules without editing their source code. The other solutions require meta-programming or modifying the original source code. I've already had one person visit our forums asking how to add an interceptor to a PyGame application, so he could unobtrusively "tap" some code.

正如他们所说,面向方面的编程比 java 更容易用 python 实现。但是 Spring Python 可以很容易地添加到现有的 Python 模块中,而无需编辑它们的源代码。其他解决方案需要元编程或修改原始源代码。我已经有一个人访问我们的论坛,询问如何向 PyGame 应用程序添加拦截器,以便他可以不显眼地“点击”一些代码。

Many people quickly assume "dependency injection" or "IoC" instantly means "XML configuration files". Not the case. While we support an XML configuration, just leap directly into using python decorators.

许多人很快就认为“依赖注入”或“IoC”的意思是“XML 配置文件”。不是这样。虽然我们支持 XML 配置,但直接使用 python 装饰器即可。

I already know about one company that is using Spring Python as a key piece of their system. They are interested in making improvements, adding new features, and generally using it as a piece of their solution. They have also experimented with running it inside jython, in case that piques your interest.

我已经知道一家公司使用 Spring Python 作为其系统的关键部分。他们有兴趣进行改进、添加新功能,并通常将其用作解决方案的一部分。他们还尝试在 jython 中运行它,以防引起您的兴趣。

At the end of the day, my suggestion is to examine all the features, and see if any of them suit your needs. Whether this is adding needless complexity or succint value can only be determined by you. You don't have to use everything; only what you need. To get some more info on what is available, I invite you to view Introduction to Spring Python, that I presented at SpringOne Americas 2008 conference.

归根结底,我的建议是检查所有功能,看看是否有任何功能适合您的需求。这是否增加了不必要的复杂性或简洁的价值只能由您来决定。您不必使用所有东西;只有你需要的。要获得有关可用内容的更多信息,我邀请您查看我在 SpringOne Americas 2008 会议上介绍的 Spring Python 简介

回答by Khan

Good stuff. I have used Spring Java, Spring Dot Net and now starting with Spring Python. Python has always been pretty easy to use for programmers; I think, especially since it's easy to write. I found Spring Dot Net to be a bit confusing, but both Spring Java and Python seem to be similar. I'm sure they have their differences, but so far at least I'm not all so confused with the Python implementation of Spring.

好东西。我使用过 Spring Java、Spring Dot Net,现在开始使用 Spring Python。对于程序员来说,Python 一直很容易使用。我认为,特别是因为它很容易写。我发现 Spring Dot Net 有点混乱,但 Spring Java 和 Python 似乎很相似。我确信它们有不同之处,但至少到目前为止,我对 Spring 的 Python 实现并不那么困惑。