SpringPython之AOP

#-*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:        ģ��1
# Purpose:
#
# Author:      Administrator
#
# Created:     07-11-2012
# Copyright:   (c) Administrator 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from springpython.context import ApplicationContext
from springpython.config import PythonConfig

from springpython.aop import *

import time
class PerformanceInterceptor(MethodInterceptor):
    def invoke(self, invocation):
        start = time.time()
        results = invocation.proceed()
        stop = time.time()
        print "Method took %2f seconds" % (stop - start)
        return results

class WikiService:
    
    def AddWhileInt(self, a, b):
        result = a+b
        total = 10000000
        while(total):
            total-=1
            result+=1
        return result
    
    def AddWhileTrue(self, a, b):
        result = a+b
        total = 10000000
        while(total>0):
            total-=1
            result+=1
        return result

class IocPythonContainer(PythonConfig):
    def __init__(self):
        super(IocPythonContainer, self).__init__()
    
    def PerformanceAdvisor(self):
        return RegexpMethodPointcutAdvisor(
                    advice = [PerformanceInterceptor()],
                    patterns = [".*.*"]
                                           )
    
    def WikiService(self):
        return ProxyFactoryObject(
                    target = WikiService(),
                    interceptors =[self.PerformanceAdvisor()]
                                  )
        
def main():
    container = IocPythonContainer()
    service = container.WikiService()
    result = service.AddWhileInt(100, 20)
    print result
    result = service.AddWhileTrue(100, 20)
    print result

if __name__ == '__main__':
    main()
        

转载于:https://www.cnblogs.com/ankier/archive/2012/11/08/2759780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值