DI/IoC
DI(Dependency Injection)
- 객체간의 결합을 느슨하게 하는 스프링의 핵심 기술
- DI/IoC는 따로 떼어서 볼 수 없다.
강결합 : 객체 간 결합도가 강한 프로그램
- HelloApp 에서 MessageBean을 직접 객체 생성하여 사용하고 있다.
- MessageBean 클래스를 다른 클래스로 변경할 경우, HelloApp의 소스를 같이 수정해 주어야 한다.
약결합 : 인터페이스를 사용하여 객체 간 결합도를 낮춘 프로그램
- HelloApp 은 MessageBean 이라는 인터페이스를 통해 객체를 사용.
- 일반적으로 팩토리 메서드를 활용하여, 사용할 객체(MessageBeanKo or MessageBeanEn) 를 생성한다. MessageBean 이라는 이름의 MessageBeanKo의 객체가 생성되든 MessageBeanEn의 객체가 생성되든 HelloApp은 수정될 사항이 없다.
<코드예시>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="korean" class="com.test03.MessageBeanKo"></bean>
<bean id="english" class="com.test03.MessageBeanEn"></bean>
</beans>
<결과>
IoC (Inversion of Control)
- 제어 역전: 프로그램의 제어 흐름 구조가 뒤바뀌는 것
Object에 있는 것을 가져와서 사용하는 것이 아니라 <bean>안의 객체로 생성하여 factory.getBean()으로 사용와서 사용한다.
의존관계를 관리하는 방법
1. Construction Injection(생성자 주입) : IoC(bean에서 생성자를 만들때)에서 값을 넣어 전달해 주는 것
<bean id="orange" class="com.test01.MessageBeanImpl">
<constructor-arg value="orange"></constructor-arg>
<constructor-arg><value>15000</value></constructor-arg>
</bean>
<!-- MessageBeanImpl orange = new MessageBeanImpl("orange", 15000); -->
2. Setter Injection: index로 순서를 잡아서 값을 넣어 주는 것
<bean id="lee" class="com.test03.Address">
<property name="name" value="이순신"/>
<property name="addr" value="서울시 강남구"/>
<property name="phone" value="010-1111-1111"/>
</bean>
<!-- property가 set의 역할을 해주고 있다.
Address lee = new Address();
lee.setName("이순신");
lee.setAddr("서울시 강남구");
lee.setPhone("010-1111-1111");
-->
<Address.java>
package com.test02;
public class Address {
private String name;
private String addr;
private String phone;
public Address() {
}
public Address(String name, String addr, String phone) {
this.name = name;
this.addr = addr;
this.phone = phone;
}
public String toString() {
return "이름: " + name + " \t 주소: " + addr + " \t 전화번호: " + phone;
}
}
<applicationContext.xml>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 이순신, 서울시 강남구, 010-1111-1111 -->
<bean id="lee" class="com.test02.Address">
<constructor-arg name="name" value="이순신"/>
<constructor-arg name="addr" value="서울시 강남구"></constructor-arg>
<constructor-arg name="phone" value="010-1111-1111"></constructor-arg>
</bean>
<!-- 홍길동, 경기도 수원시, 010-2222-2222 -->
<bean id="hong" class="com.test02.Address">
<constructor-arg index="0" value="홍길동"></constructor-arg>
<constructor-arg index="1" value="경기도 수원시"></constructor-arg>
<constructor-arg index="2" value="010-2222-2222"></constructor-arg>
</bean>
</beans>
<Mtest.java>
package com.test01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mtest {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("com/test01/applicationContext.xml"); // new --가 경로를 가져와서 인스턴스를 만들어 준다.
MessageBean strawberry = (MessageBean) factory.getBean("strawberry");
strawberry.sayHello("김선아");
MessageBean orange = (MessageBean) factory.getBean("orange");
orange.sayHello("최희선");
MessageBean banana = (MessageBean) factory.getBean("banana");
banana.sayHello("장성윤");
}
}
3. Field Injection: annotation, autowired를 활용하여 값을 넣어주는 방식
'Web > Spring' 카테고리의 다른 글
[Spring]Autowire (0) | 2020.10.23 |
---|---|
[Spring]MessageSourceAware (0) | 2020.10.23 |
[Spring]constructor injection_setter injection (0) | 2020.10.23 |
[Spring]Maven (0) | 2020.10.22 |
[spring]기본설명 (0) | 2020.10.19 |