Python依赖注入Fram

2024-04-25 01:44:08 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Python,是否有一个相当于Guice(http://code.google.com/p/google-guice)的框架?


Tags: com框架httpgooglecodeguice
3条回答

我喜欢这个简单整洁的框架。

http://pypi.python.org/pypi/injector/

Dependency injection as a formal pattern is less useful in Python than in other languages, primarily due to its support for keyword arguments, the ease with which objects can be mocked, and its dynamic nature.

That said, a framework for assisting in this process can remove a lot of boiler-plate from larger applications. That's where Injector can help. It automatically and transitively provides keyword arguments with their values. As an added benefit, Injector encourages nicely compartmentalized code through the use of Module s.

While being inspired by Guice, it does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.

Spring Python是基于Java的Spring框架和Spring安全性的分支,面向Python。此项目当前包含以下功能:

  • Inversion Of Control (dependency injection)-使用经典的XML或python@Object装饰器(类似于Spring JavaConfig子项目)将事情连接在一起。虽然@Object格式与Guice样式(每个类中的集中式连接与连接信息)不同,但它是连接python应用程序的一种有价值的方法。
  • Aspect-oriented Programming-在水平编程范型(而不是垂直OOP继承)中应用拦截器,用于事务、安全性和缓存等。
  • DatabaseTemplate-从数据库读取需要打开游标、读取行和关闭游标以及异常处理程序的单调循环。使用这个模板类,您只需要SQL查询和行处理函数。剩下的工作由Spring Python完成。
  • 数据库事务-用事务包装多个数据库调用会使代码难以读取。此模块提供多种方法来定义事务,而不会使事情变得复杂。
  • 安全性-插件安全拦截器,利用身份验证和域授权锁定对方法的访问。
  • 远程处理-很容易将本地应用程序转换为分布式应用程序。如果已经使用IoC容器构建了客户机和服务器部件,那么从本地到分布式只是配置更改。
  • 示例-为了帮助演示Spring Python的各种特性,创建了一些示例应用程序:
    • PetClinic-Spring框架的示例web应用程序是使用pythonweb容器从头开始重建的,这些容器包括:CherryPy。去看看如何使用这个框架的例子。(注意:其他python web框架将在将来添加到此列表中)。
    • Spring Wiki-Wiki是存储和管理内容的强大方法,因此我们创建了一个简单的示例!
    • Spring Bot-使用Spring Python构建一个小型Bot来管理开源项目的IRC通道。

我还没有使用它,但是Spring Python框架是基于Spring并实现Inversion of Control

Python项目中似乎还有一个Guice:snake-guice

相关问题 更多 >