Package: hehey-hcontainer
hehey-hcontainer介绍hehey-hcontainer 是一个python di 容器,提供依赖注入等等功能依赖以及版本要求python >= 3.5安装直接下载:命令安装：pip install hehey-hcontainer基础文件以目录参数配置fromhcontainer.base.DefinitionimportDefinitionbeanConf={'class':'site.service.User.User',# 类路径'_single':True,# 是否单例,默认是单例,'_scope':'app',# 对象作用域, app应用作用域，request 请求作用域'_init':'init',# 初始化方法, 对象创建后调用设置的方法(设置属性完成后调用)'_args':[],# or True 构造方法参数，支持索引，关联数组'_attr':[],# 类其他属性，直接注入'attr1':'属性1','attr2':'属性2','attrBean':'<func::bean>',# 属性调用函数, 自动调用func函数(bean) 获取属性值,'addresstBean':'<ref::address|天收你>',# 属性对应另一个bean(user),| 之后为bean 的参数'attr3':Definition({'_ref':'user'# attr3 为另一个bean为user的对象})}基本示例快速使用fromhcontainer.beanimportBeanManagercomponents={# 构造器注入'user':{'clazz':'site.service.User.User','_args':['hehe 小酌一杯'],},# 属性注入'address':{'clazz':'site.service.Address.Address','realName':'hehe','user':'<ref::user>'},}beanManager=BeanManager.make(components);# 获取bean 实例user=beanManager.getBean('user')address=beanManager.getBean('address')属性注入fromhcontainer.beanimportBeanManagerclassUser:def__init__(self,**attrs):self.username=''ifattrs:#设置属性passreturn;defgetUsername(self):returnself.usernamecomponents={# 构造器注入'user':{'clazz':'site.service.User.User','username':'hehe',},}beanManager=BeanManager.make(components);user=beanManager.getBean('user')构造参数注入# 示例1classUser:def__init__(self,**attrs):self.username=''ifattrs:#设置属性passself._init();return;def_init(self):# 初始化操作return;defgetUsername(self):returnself.username# 此配置针对构造参数只有**attrscomponents={# 构造器注入'user':{'clazz':'site.service.User.User','_args':True,'username':'hehe',},}# 示例2classAddress:def__init__(self,address='',city=''):self.addr=addressself.city=cityself._user=None;@propertydefuser(self):returnself._usercomponents={# 构造器注入'user':{'clazz':'site.service.User.User','_args':['我的收货详细地址','上海'],},}bean 属性值为另一beanfromhcontainer.beanimportBeanManagerclassUser:def__init__(self):self.username=''self.address=None;return;defgetAddress(self):returnself.addressclassAddress:def__init__(self,address='',city=''):self.addr=addressself.city=citydefgetAddr(self):returnself.addrcomponents={'user':{'clazz':'site.service.User.User','username':"hehe 小酌一杯",'address':'<ref::address>'},# 属性注入'address':{'clazz':'site.service.Address.Address','address':'深圳','city':'上海',}}beanManager=BeanManager.make(components);user=beanManager.getBean('user')address=user.getAddress()address.getAddr()初始化方法(对象创建后,自动调用指定的方法)classUser:def__init__(self,**attrs):self.username=''ifattrs:#设置属性passself._init();return;def_init(self):# 初始化操作print("初始化对象自动调用此方法!")return;defgetUsername(self):returnself.usernamecomponents={# 构造器注入'user':{'clazz':'site.service.User.User','_init':"_init",'username':'hehe',},}装饰器注解(id,ref)类为bean 对象fromhcontainer.beanimportBeanManagerfromhcontainerimportbean@bean.id("addrbean")classAddress:def__init__(self,address=''):self.addr=addressdefgetAddr(self):returnself.addr@bean.id("userbean")classUser:def__init__(self,username='nnnn'):self.username=usernameself.__address=None;@property@bean.ref('addrbean',name='__address')defaddress(self):returnself.__addressdefgetName(self):returnself.usernamebeanManager=BeanManager.make();user=beanManager.getBean('user')创建新的bean 实例fromhcontainer.beanimportBeanManagercomponents={# 构造器注入'user':{'clazz':'site.service.User.User','_init':"_init",'username':'hehe',},}beanManager=BeanManager.make(components);# 新对象,非单例user=beanManager.makeBean('user')根据类路径创建bean 对象fromhcontainer.beanimportBeanManagerbeanManager=BeanManager.make();user=beanManager.getBean('site.service.User.User')bean 作用域创建bean容器fromhcontainer.beanimportBeanManagerbeanManager=BeanManager.make();requestContainer=beanManager.makeContainer('request')sessionContainer=beanManager.makeContainer('session')