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