Learning IoC

Inversion of Control

1. Hollywood's principle: Don't call me, I'll call you
2. Instead of lookup the resource we want. we leave it to the framework

 

Traditional 'pull approach'

Context ctx = new InitialContext();
AccountEJB accountEJB = (AccountEJB) ctx.lookup("java:comp/env/ejb/AccountEJB");

Drawback:
1. client codes tied to a specific implementation
2. codes are difficult to test (must be tested within the container)

 

IoC Approach:

private Account accountManager;
public void setAccountManager(Account account) {
      this.accountManager = account;
}

Addvantage:
1. client code does not need to deal with component discovery
2. client code isn't tied to any specific implementations
3. codes can Unit Testing?individually with mock object

 

3 Types:


Type 1: interface injection?

dependency satisfied by interfaces it implemented


Type 2: setter injection

dependency satisfied by JavaBean properties and its setter exposed by a component


 Type 3: constructor injection

dependedcy satisfied by the component's constructor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值