spring python_python菜鸡对java spring的初步理解

百度一哈Spring入门,最常见的两个概念就是IOC(控制反转)和依赖注入(DI)。看了两天资料,简单说下自己的理解。首先整体的大思想是解耦,具体一点是类本身与数据(依赖)的解耦,以及整体流程的分层。

IOC 与 DI

所谓IOC,即是指将类和数据等解耦,把类的一切控制权交给容器。所谓容器,是控制管理类或者bean的地方。容器来为类分配数据,控制类的行为。

在最常见的helloworld例子里,为了输出helloworld,我们先实现了一个类,这个类本身的作用是用来输出一个message。然后这个message,也就是helloword这个字符串,被存放在了xml的配置文件中。

就是数据和逻辑的分离。

而把helloworld传递给message赋值,就叫做依赖注入。之所以依赖注入要单独拿出来。是因为一个类的初始化赋值有很多方式。毕竟配置文件写大了,容易乱。一个bean可能被用在好几个地方,一个类的初始化也要注入好多bean。

Spring的依赖注入和切面,其实和python的装饰器很像

而且写法上都是@

def inject_message(cls, message):

cls.message = message

@inject_message('hello')

class Printer(object):

message = ''

def print_message(self):

print self.message

是不是一样的感觉?

只不过bean里面,所有的数据都是注入的,所以管理起来更麻烦一些,也不会像这个装饰器这样这么优雅。

至于切面,就更是装饰器了。

AOP

所谓的before,after就是@before(func),只不过我们一般装饰器写的不会这么通用罢了,而且装饰器本来就是before after都包括了。当然拆分的更细的话,更通用的话,最后应该是就是这个样子吧。

这是从思想上,spring给我的感觉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# Author: Greg Lee Turnquist # Paperback: 264 pages # Publisher: Packt Publishing; 1st New edition edition (May 21, 2010) # Language: English # ISBN-10: 1849510660 # ISBN-13: 978-1849510660 Amazon 的介紹: http://www.amazon.com/Spring-Python-1-1-Greg-Turnquist/dp/1849510660 Spring Python captures the concepts of the Spring Framework and Spring Security and brings them to the world of Python and provides many functional parts to assemble applications. Spring Python is all about using the many useful features of Spring to the fullest and making these features available when working with Python. Get to grips with all of the concepts of Spring and apply these to the language and environment of Python to develop powerful applications for your own personal requirements. The book provides an introduction to Spring Python and steadily takes you towards the advanced features that this integration has to offer. Spring uses the Java programming language. Spring Python, the first Spring extension to go live, allows developers to make maximum use of Spring features in Python. This book starts off by introducing each of the core building blocks of Spring Python using real code examples and high-level diagrams. It explores the various concepts of Spring Python with the help of examples and case studies and focuses on vital Spring Python features to make the lives of Python and Java developers simple. The early chapters cover simple applications with simple operations including data access, and then subsequent chapters scale up to multi-node, secured, transactional applications stopping short of very advanced level complexity. This book will help you to scale your applications without having to add unnecessary complexity

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值