1. IoC(Inversion of Control)란?
'제어역행'이라 불리며, IoC가 적용되면 객체는 의존하는 다른 객체를 생성 및 찾는 대신 수동적으로 의존성을 받는다. 스프링 프레임워크는 제어역행(IoC)을 통해 애플리케이션의 느슨한 결합을 도모한다.
2. IoC 실습하기
- ioc3.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">
<bean class="edu.blz.ioc3.Money" id="money">
<property name="amount">
<value>2000</value>
</property>
</bean>
<bean class="edu.blz.ioc3.Car" id="car">
<property name="name">
<value>k7</value>
</property>
</bean>
<bean class="edu.blz.ioc3.KiaMaker" id="kiaMaker">
<property name="car">
<ref bean="car" />
</property>
</bean>
<bean class="edu.blz.ioc3.HyunDaiMaker" id="hyunDaiMaker">
<property name="car">
<bean class="edu.blz.ioc3.Car">
<property name="name" value="sonata" />
</bean>
</property>
</bean>
<bean class="edu.blz.ioc3.DaewooMaker" id="daewooMaker">
<property name="car">
<bean class="edu.blz.ioc3.Car">
<property name="name" value="tosca" />
</bean>
</property>
</bean>
<bean class="java.lang.String" id="tosca">
<constructor-arg value="tosca" />
</bean>
<bean class="edu.blz.ioc3.OrderManager" id="orderManager">
<property name="maker">
<ref bean="hyunDaiMaker" />
</property>
<property name="money">
<ref bean="money" />
</property>
</bean>
</beans>
- ioc4.xml(context 적용)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="edu.blz.ioc4" />
</beans>
3. IoC 실습 소스 코드
- Car.java
package edu.blz.ioc3;
public class Car {
private String name;
public Car() {
}
public Car(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- CarMaker.java
package edu.blz.ioc3;
public interface CarMaker {
Car sell(Money money);
}
- DaewooMaker.java
package edu.blz.ioc3;
public class DaewooMaker implements CarMaker {
private Car car;
public void setCar(Car car) {
this.car = car;
}
public Car sell(Money money) {
return car;
}
}
- HyunDaiMaker.java
package edu.blz.ioc3;
public class HyunDaiMaker implements CarMaker {
private Car car;
public void setCar(Car car) {
this.car = car;
}
public Car sell(Money money) {
return car;
}
}
- KiaMaker.java
package edu.blz.ioc3;
public class KiaMaker implements CarMaker {
private Car car;
public void setCar(Car car) {
this.car = car;
}
public Car sell(Money money) {
return car;
}
}
- Money.java
package edu.blz.ioc3;
public class Money {
private int amount;
public Money() {
}
public Money(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
- OrderManager.java
package edu.blz.ioc3;
public class OrderManager {
private CarMaker maker;
private Money money;
public void setMaker(CarMaker maker) {
this.maker = maker;
}
public void setMoney(Money money) {
this.money = money;
}
public void order() {
Car car = maker.sell(money);
System.out.println("I sold a car :: " + car.getName() + ", " + money.getAmount());
}
}
- OrderManagerApp.java
package edu.blz.ioc3;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class OrderManagerApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("ioc3.xml");
OrderManager manager = ctx.getBean("orderManager", OrderManager.class);
manager.order();
ctx.close();
}
}