Following Toby's Spring 3.1 in Spring Boot :
Chapter 1 Objects and Dependency Relationships - 1.7 DI Dependency Injection
Development environment - OS : mac - STS : 4.0.1 - MySQL : Server version: 8.0.13 Homebrew - Frame-Work : for now, going with jar only, to run with as few dependencies as possible Related links - KSUG Homepage http://www.ksug.org/ - KSUG Q&A https://groups.google.com/forum/#!topic/ksug/13vB4tCFqrI - Spring Camp 2017 https://www.youtube.com/playlist?list=PLdHtZnJh1KdZ6NDO9zc9hF4tONDLTSEUV |
Chapter 1: Objects and Dependency Relationships
1.7 DI Dependency Injection
Starting from the somewhat broad term "IoC container," let's now look at the newer (or newly named) concept — Dependency Injection dependency injection DI.
1.7.0 previously (pree-vee-us-lee)
Summarizing what we've seen so far: IoC is not something used only in Spring, but a general concept frequently found in software overall.
And we practiced several examples that reflect the IoC concept: creating objects through a factory class and wiring their relationships, the template method pattern that embeds that same basic idea, and a service-container style running on the server (similar to a servlet container), among others.
1.7.1 Inversion of Control (IoC) and Dependency Injection
Since Spring is itself a container and a framework, you could say all of its fundamental workings are based on IoC. But the feature that clearly differentiates Spring from other frameworks becomes clear when we use the new term dependency injection.
1.7.2 Setting runtime dependency relationships
1. Dependency relationships
Time to understand the basic concept.
Personally, I can't quite get used to the direction of UML arrows. I don't know why, but I keep reading them the other way around. So I'm redefining them my own way.
I've decided to think of the arrow as Cupid's love-arrow. Ah... then it clicks immediately. Right now A has a one-sided crush on B... ㅜㅜ
2. UserDao's dependency relationships
Time to understand it by applying it to our previous example.
1.7.3. Dependency Injection for UserDao
1) First, let's revisit DaoFactory
package springbook.user.dao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DaoFactory {
// UserDao 타입의 오브젝트를 생성하고 초기화해서 돌려주는 역할 담당
@Bean
public UserDao userDao() {
UserDao userDao = new UserDao(connectionMaker());
return userDao;
}
//ConnectionMaker 타입의 오브젝트를 생성하는 역할 담당
@Bean
public ConnectionMaker connectionMaker() {
return new DConnectionMaker();
}
}
2) Class/code-level dependency relationships : This is the code I wrote before IoC.
public class UserDao {
// 관계설정 책임 분리 전의 생성자
public UserDao( ) {
connectionMaker = new DConnectionMaker( );
}
... 중략
}
3) Runtime dependency injection and usage dependency relationships: This is the code after applying IoC.
public class UserDao {
private ConnectionMaker connectionMaker;
// 두 오브젝트 사이의 런타임 의존관계를 설정해주는 의존관계 주입 자업을 전도하는 존재이며,
// 동시에 IoC방식으로 오브젝트의 생성과 초기화, 제공 등의 작업을 수행하는 컨테이너
// = 의존관계 주입을 담당하는 컨테이너 = DI 컨테이너 = IoC/Di 컨테이너,
// DaoFactory는
// "UserDao를 생성하는 시점에" + 주입(파라미터로 만들어진 DConnectionMaker의 오브젝트의 레퍼런스를 전달)한다
public UserDao(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}
... 중략
}4) DI fits the IoC concept in that it hands off the selection and creation control of the object it will use to an external party, while passively using the object it is given.
5) The IoC provided by the Spring container is mainly focused on dependency injection, or DI. That is why Spring is also called a DI container or DI framework, in addition to an IoC container.
1.7.3. Dependency lookup and dependency injection
(This is getting long... I'll continue next time.)
