Learning IOC

zhaozj2021-02-16  89

Inversion of Control1. Hollywood's Principle: Don't call me, I'll call you2. 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 Implementation2. Codes Are Difficult To Test (Must Be Tested within the Container)

IOC Approach:

Private Account AccountManager; Public Void SetAccountManager (Account Account) {this.accountManager = Accent;}

AddVantage: 1. Client Code Does Not Need To DEAL WITH COMPONENT Discovery2. Client Code Isn't Tied to Any Specific Implementations3. 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 SETER EXPOSED BY A Component

Type 3: Constructor Injection

Dependedcy Satisfied by The Component's Construction

转载请注明原文地址:https://www.9cbs.com/read-11704.html

New Post(0)