06.스프링으로 di설정하기
스프링으로 DI 설정하기
Spring.di에 Sring Bean Configuration File을 만든다
스프링에게 지시하는 방법으로 코드를 변경해보자
Exam exam = new newlecExam();
ExamConsole console = new GridExamConsole(exam);
console.setExam(exam);
console.print();
스프링은 위의 관계를 설정해서 부품을 던져주는 기능-> 설정을 분리하겠다 지시서 이기때문에 스프링은 자바코드로 사용하지 않는다
<?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">
<!--Exam exam = new newlecExam(); -->
<bean id = "" class=""/>
</beans>
class: 어떤 클래스를 객체로 만들 것인지
id: 그 객체를 어떠한 이름으로 쓸 것인지(변수명)
- 주의사항이 있다
같은 동명이인 같은게 존재 할 수있다
따라서 패키지명까지 써야한다
<?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">
<!--Exam exam = new newlecExam(); -->
<bean id = "Exam" class="Spring.di.entity.newlecExam"/>
<!--ExamConsole console = new GridExamConsole(exam);-->
<bean id ="console" class="Spring.di.ui.GridExamConsole">
<!--console.setExam(exam); -->
<property name="exam" ref="exam"/>
</bean>
</beans>
console이 exam 가진다
이떄 setter을 사용하기 위해 프로퍼티를 이용하여 사용해야한다
프로퍼티를 사용해서 setExam을 설정한다
프로퍼티명으로 쓰겠다 묵시적으로 setExam을 쓰겠다
exam으로 바꾼다(대문자를 소문자로)
property name="exam" value ="" ref=""/>
이때 setter 에 설정 할 수 있는게 2가지 있는데 exam의 이름을 써준다
참조형이면 ref 사용하고 value타입이면 value을 사용한다
댓글남기기