.XML 파일이 MyInfo 클라스를 참조하는 객체를 만들었다.
.XML 에 property name 이 MyInfo 클래스에 멤버변수로 들어간다.
value 가 MyInfo 클래스 인자값으로 들어가서 멤버변수 값이 된다.
<?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 id ="bmiCalculator" class="com.javalec.ex.BMICalculator">
<property name="lowWeight">
<value>18.5</value>
</property>
<property name="normal">
<value>23</value>
</property>
<property name="overWeight">
<value>25</value>
</property>
<property name="obesity">
<value>30</value>
</property>
</bean>
<bean id="myInfo" class="com.javalec.ex.MyInfo">
<property name="name">
<value>홍길동</value>
</property>
<property name="height">
<value>187</value>
</property>
<property name="weight">
<value>84</value>
</property>
<property name="hobbys">
<list>
<value>수영</value>
<value>요리</value>
<value>독서</value>
</list>
</property>
<property name="bmiCalculator">
<ref bean = "bmiCalculator"/>
</property>
</bean>
</beans>
bean 으로 객체를 두개 생성합니다
BMICalculator / bmiCalculator
MyInfo /myInfo
BMICalculator
안에 property name 으로 lowWeight 하면
BMICalculator 멤버변수 lowWeight 을 참고하겠다 입니다.
value 는 MBICalculator setLowWeight의 인자값으로 value를 넣겠다 입니다.
그럼 .xml 에서 얻은 값이 BMICalculator 의 멤버변수가 됩니다.
MyInfo 도 같습니다.
property bmiCalculator 는 이름이고
ref bean 에 bmiCalculator 는 해당 객체를 참고해서 자료형을 사용할 수 있습니다
package com.javalec.ex;
public class BMICalculator {
private double lowWeight;
private double normal;
private double overWeight;
private double obesity;
public void bmicalculation(double weight, double height) {
double h = height * 0.01;
double result = weight/ (h*h);
System.out.println("BMI 지수 :" +(int)result);
if(result > obesity) {
System.out.println("비만입니다");
}else if(result > overWeight) {
System.out.println("과체중 입니다");
}else if(result > normal) {
System.out.println("정상입니다");
}else {
System.out.println("저체중 입니다");
}
}
public void setLowWeight(double lowWeight) {
this.lowWeight = lowWeight;
}
public void setNormal(double normal) {
this.normal=normal;
}
public void setOverWeight(double overWeight) {
this.overWeight = overWeight;
}
public void setObesity(double obesity) {
this.obesity = obesity;
}
}
자바 코드가 특별히 볼건 없는데, 멤버변수 setter 가 필요한건 기억해야 합니다
package com.javalec.ex;
import java.util.ArrayList;
public class MyInfo {
private String name;
private double height;
private double weight;
private ArrayList<String> hobbys;
private BMICalculator bmiCalculator;
public BMICalculator getBmiCalculator() {
return bmiCalculator;
}
public void setBmiCalculator(BMICalculator bmiCalculator) {
this.bmiCalculator = bmiCalculator;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setHeight(double height) {
this.height = height;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setHobbys(ArrayList<String> hobbys) {
this.hobbys = hobbys;
}
public void bmicalculation() {
bmiCalculator.bmicalculation(weight,height);
}
public void getInfo() {
System.out.println("이름 : "+ name);
System.out.println("키 : "+ height);
System.out.println("몸무게 : "+ weight);
System.out.println("취미: "+ hobbys);
bmicalculation();
}
}
메소드 bmicalculation 이 bmiCalculator 메소드를 통해 bmicalculation 메소드를 호출합니다.
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
String configLocation = "classpath:applicationCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation);
MyInfo myInfo = ctx.getBean("myInfo",MyInfo.class);
myInfo.getInfo();
ctx.close();
}
}
메인 클래스 입니다
문자열로 applicationCTX.xml 경로를 configLocation 에 저장합니다.
GenericXmlApplicationContext 에 configLocation 을 넣어서 , 만들어진 객체 참고,
설정을 하게 합니다. 해당 객체는 getBean을 가지고 있습니다. myInfo는 이미 .xml 에서
읽어와서 객체명을 읽어 올수 있습니다. 해당 객체에 맞는 클래스를 적어서 클래스 객체를 만들어 줍니다.
이 부분을 잘 이해 해야 합니다