스프링에게 전달한 조립 지시서를 만들어야 함
-> XML 파일, 어노테이션 형태
1. 패키지 우클릭
2. New - Others - Spring Bean Configuration File 선택
3. 지시서로 사용할 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">
</beans>
Exam.java
package spring.di.entity;
public interface Exam {
int total();
float avg();
}
KimExam.java
package spring.di.entity;
public class KimExam implements Exam {
private int kor;
private int eng;
private int math;
private int com;
@Override
public int total() {
// TODO Auto-generated method stub
return kor+eng+math+com;
}
@Override
public float avg() {
// TODO Auto-generated method stub
return total()/4.0f;
}
}
ExamConsole
package spring.di.ui;
import spring.di.entity.Exam;
public interface ExamConsole {
void print();
void setExam(Exam exam);
}
GridExamConsole.java.
package spring.di.ui;
import spring.di.entity.Exam;
public class GridExamConsole implements ExamConsole {
private Exam exam;
public GridExamConsole() {
}
public GridExamConsole(Exam exam) {
this.exam = exam;
}
@Override
public void print() {
System.out.println("┌─────────┬─────────┐");
System.out.println("│ total │ avg │");
System.out.println("├─────────┼─────────┤");
System.out.printf("│ %3d │ %3.2f │\n",exam.total(),exam.avg());
System.out.println("└─────────┴─────────┘");
}
@Override
public void setExam(Exam exam) {
// TODO Auto-generated method stub
this.exam = exam;
}
}
InlineExamConsole.java
package spring.di.ui;
import spring.di.entity.Exam;
public class InlineExamConsole implements ExamConsole {
private Exam exam;
public InlineExamConsole() {
}
public InlineExamConsole(Exam exam) {
this.exam = exam;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.printf("total is %d, avg is %f\n",exam.total(),exam.avg());
}
@Override
public void setExam(Exam exam) {
// TODO Auto-generated method stub
this.exam = exam;
}
}
Program.java
package spring.di;
import spring.di.entity.Exam;
import spring.di.entity.KimExam;
import spring.di.ui.ExamConsole;
import spring.di.ui.GridExamConsole;
import spring.di.ui.InlineExamConsole;
public class Program {
public static void main(String[] args) {
Exam exam = new KimExam();
ExamConsole console = new GridExamConsole();
console.setExam(exam); // 생성자와 set메서드를 통한 결합
console.print();
}
}
Interface를 만들어 사용해 결합도를 낮춘다.
스프링 사용 시 변경점
Exam exam = new KimExam();
ExamConsole console = new GridExamConsole();
console.setExam(exam);
이 부분을 스프링에게 맡기게 된다.
setting.xml
<bean id="exam" class="spring.di.entity.KimExam" />
<bean id="console" class="spring.di.ui.GridExamConsole">
<property name="exam" ref="exam"></property>
</bean>
bean
property
해당 객체의 필드들을 초기화할 수 있다.
<bean id="exam" class="spring.di.entity.KimExam" />
가 reference기 때문에 ref 사용<property name="exam" ref="exam"></property>
<property name="exam" value="30"></property>
다음과 같은 형태도 사용 가능
<property name="exam">
<value>30</value>
</property>