关于ServiceLocator ,AdpaterAwareInterface 注入

Posted coco

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于ServiceLocator ,AdpaterAwareInterface 注入相关的知识,希望对你有一定的参考价值。

今天学习zf2 的过程,视频中讲到要把数据库的中的表继承TableGateway中,然后注册在ServiceManager中,但是里面没有$adapter,需要在serviceManger,中配置initializer,添一个类,这个类实现intializerInterface,里面有个initialize方法,判断如果服务产生的类实现了AdapterInterface,则执行setDbAdapter(),将SerivcieLocator->get(‘adpater‘)注入到该类,这样就实现在调用该userTable服务时,自动注入了$adpater.

然后,我想的是如果userTable直接实现serviceLocatorAwareInterface不是也可把adater注入进来吗,于是,试了一下,serviceLocator能进来,但是该服务在是要直接产生实例的,没办法把serviceLocator给$adapter,所以还得实现AdapterInterface,初始化把它注入进去

以上是关于关于ServiceLocator ,AdpaterAwareInterface 注入的主要内容,如果未能解决你的问题,请参考以下文章

关于ZF2中一点感悟,service_manager

两种配置adpater的方式

Get IP Address for different Adpater

WPF之Unity与ServiceLocator运用

zf2 ServiceManager vs ServiceLocator

如何使用ZF3设置延迟加载(从任何地方都没有ServiceLocator模式)