ApplicationContext context = new ClassPathXmlApplicationContext
package spring.di.entity;
public interface Exam {
public int total();
public float avg();
}
package spring.di.entity;
public class NewlecExam implements Exam {
private int kor;
private int eng;
private int math;
private int com;
public void setCom(int com) {
this.com = com;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getCom() {
return 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;
}
}
package spring.di.ui;
import spring.di.entity.Exam;
public interface ExamConsole {
public void print();
public void setExam(Exam exam);
}
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("│"+exam.total()+"│"+exam.avg()+"│");
System.out.println("└──────────────────┘");
}
@Override
public void setExam(Exam exam) {
this.exam = exam;
}
}
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() {
System.out.printf("total is InLine %d, %f", exam.total(), exam.avg());
}
@Override
public void setExam(Exam exam) {
this.exam = exam;
}
}
<?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>
<?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(); -->
<bean id="console" class="spring.di.ui.InLineExamConsole">
<!-- console.setExam(exam); -->
<property name="exam" ref="exam"></property>
</bean>
</beans>
<property name="exam" ref="exam"></property>
property name="exam"
setExam -> set을 빼고 E를 소문자로 바꾸는 것이 약속setter
가 꼭 존재해야 함.id=""
로 가져오기 때문에 어떠한 객체인지 알 수 없기에 object 형식
으로 꺼내옴 -> 형변환ExamConsole console = (ExamConsole)context.getBean("console");
ExamConsole console = context.getBean(ExamConsole.class);
Spring