有冇类似于spring的框架?

问题

paul <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Sun, May 25, 2008 at 10:34 AM
subject [CPyUG:52386] python 有没有类似于spring的框架

如题,最近写了一些python程序,感觉如果模块比较多,他们之间互相引用会导致耦合度会增加,维护起来感觉不太爽,想问问python有没有类似于java的spring这样的框架!

-- Boern Parx

回答

沈崴 <[email protected]>
reply-to        [email protected]
to      python-cn`CPyUG`华蟒用户组 <[email protected]>
date    Sun, May 25, 2008 at 10:44 AM

有。

手工

junyi sun <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Sun, May 25, 2008 at 1:44 PM

手动啊,不需要applicationContext.xml这种配置文件了,写一个py把所有模块组装起来就行了,我认为DI只是一种思想,在python里面实现起来反而更简单。

比如:

   1 class ServiceA(object):
   2       def __init__(self,dao):
   3            self.dao = dao
   4      
   5       def foobar(self,x):
   6            self.dao.insert(x)   #不需要定义接口,动态语言保证了这在语法上是可以通过的
   7  
   8 class UserDAO(object):
   9       def __init__(self,conn):
  10             self.conn = conn
  11  
  12       def insert(self,x):
  13             self.conn.executeSql("insert into user(..) values (...)" % (x,))
  14  
  15  
  16 if __name__ == "__main__":
  17        conn = DBConnection(conf)
  18        dao = UserDAO(conn)
  19        service = ServiceA(dao)
  20        #上面这几行就完成了Spring的DI功能,反而比Java简单
  21  
  22        service.foobar("i love python")


反馈

创建 by -- ZoomQuiet [2008-05-25 07:32:12]

MiscItems/2008-05-25 (last edited 2009-12-25 07:16:01 by localhost)