Spring Ioc DI
포스트
취소

Spring Ioc DI

DI 란

DI는 Dependency Injection으로 IoC 가 추구하는 객체 주입 디자인 패턴 중의 한 가지입니다 그럼 소스를 한번 보면서 살펴보겠습니다

Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Student {
	
	private String name;
	
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
}

ClassRoomA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ClassRoomA {
		
	private Student student;
	
	public ClassRoomA(Student student) {
		
		this.student = student;
		
	}
	
	public void classRoomAInStudent() {
		
		System.out.print(student.toString());
	}
}


2개의 소스코드가 보이는데 현재 ClassRoomA 클래스는 현재 Student를 의존하고 있으며 이때 의존 방식은 ClassRoomA 객체를 생성할 때 Student 객체를 넣어주면서 생성을 하게 됩니다 이때 java에서는 단순히 이런 소스에 대해서는 이와 같이 코드를 만들어가면 됩니다

java 에서 값을 주입할때

1
2
3
4
5
6
7
8
Student student = new Student();
student.setName("time");
student.setAge(20);

ClassRoomA roomA = new ClassRoomA(student);
roomA.classRoomAInStudent();

이렇게 Student 객체를 만들고 각각의 값을 세팅한 뒤에 ClassRoomA 객체를 생성할 때 생성자에 넣어주면 객체 생성이 완료가 됩니다 이 코드 또한 현재 ClassRoomA 가 Student를 의존하고 있다고 말할 수 있습니다 이 또한 DI입니다 단 이때는 Ioc 컨테이너가 DI를 한 것이 아니라 우리가 DI를 했다고 볼 수 있습니다 우리는 이렇게 앞으로 객체를 만들지 않을 예정이기에 이를 Ioc 컨테이너 bean의 형태로 만든 다음 불러오도록 하겠습니다

bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
		
	  	<bean id = "student" class="com.cybb.main.Student">
	  		<property name="name" value="time"/>
	  		<property name="age" value="20"/>
	  	</bean>
	  	
	  	<bean id = "classRoomA" class="com.cybb.main.ClassRoomA">
	  		<constructor-arg ref = "student"/>
	  	</bean>
    </beans>

bean 정의서는 이렇게 작성을 할 예정입니다 그리고 이때 각각 필요한 값은 setter는 <property name="name" value="time"/> property를 이용해서 name에 time이라는 값을 넣어주는 것이고 생성자에 주입을 할 때에는 <constructor-arg ref = "student"/> 주입을 하게 되는데 이때는 ref 참조 즉 이미 생성된 bean 을 참조하는 것으로 student를 넣어주는 것을 말합니다 이때도 IoC 컨테이너의 DI 가 일어나게 되는데 우리가 직접 new 객체를 불러와서 할 때는 직접 setter 함수를 불러서 값을 집어넣은 반면 우리는 정의만 해두면 차후 이 bean 이 사용할 때 IoC 컨테이너가 해당 값들을 집어넣게 되는데 이 또한 Ioc에 의한 DI라고 할 수 있습니다 이렇게 되면 bean 정의는 완료가 되었고 우리는 이를 컨테이너에 넣고 동작을 보겠습니다

IoC 컨테이너에 주입

1
2
3
4
5
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
ClassRoomA roomA = applicationContext.getBean("classRoomA" , ClassRoomA.class);
roomA.classRoomAInStudent();
		

컨테이너 넣는 방식은 앞에서 본 것과 같이 ClassPathXmlApplicationContext 정의한 후 bean의 위치를 명시한 이름을 넣은 다음 ClassRoomA 객체를 만드는 것으로 끝 여기서는 Ioc 컨테이너가 직접 Student 객체를 ClassRoomA 객체에 의존성을 부여하게 됩니다 여기서 앞에 하고는 다른 Ioc DI 가 발생을 하는 것이죠