Dependency Injection(DI):
DI flavor of Inversion of Control. The Inversion of Control(IoC) is a general concept, and it can be expressed in many different ways and Dependency Injection is merely one concrete example of Inversion of Control.
Dependency Injection helps in gluing these classes together and same time keeping them independent.
Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods.
Inversion of Control(IoC)
優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
把對(duì)象生成放在了XML里定義,使換一個(gè)實(shí)現(xiàn)子類變得很簡(jiǎn)單(用于實(shí)現(xiàn)某種接口的對(duì)象), 只要修改XML就可以,甚至可以實(shí)現(xiàn)對(duì)象的熱插拔。
缺點(diǎn):
1)生成對(duì)象步驟變復(fù)雜
2)使用反射編程,效率損耗。(損耗<提高的靈活性和維護(hù)性)
3)缺少IDE重構(gòu)操作支持
實(shí)現(xiàn)方式:
實(shí)現(xiàn)數(shù)據(jù)訪問(wèn)層
模塊與接口重構(gòu)
隨時(shí)增加單元測(cè)試
使用服務(wù)定位器而不是構(gòu)造注入
Spring Architecture
