-
[Spring] DI 기본예제 1 - BMI 계산기Web/Spring 2022. 10. 25. 11:26
- DI란?
DI는 Dependency Injection의 줄임말, 의존관계 주입이라고 한다.
여기서 의존관계 주입이란, 어떠한 대상이 변하면 그것에 의존하는 대상 또한 영향을 미친다는 의미이다.
예를 들어, A라는 친구가 B에 의존하는데, B가 바뀌면 A 친구 또한 영향이 미친다는 뜻이다.
- BMI 계산기 구현하기
< MyInfo.java >
package com.javalec.ex; import java.util.ArrayList; public class MyInfo { private String name; private double height; private double weight; private ArrayList<String> hobby; private BMICal bmiclass; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public ArrayList<String> getHobby() { return hobby; } public void setHobby(ArrayList<String> hobby) { this.hobby = hobby; } public BMICal getBmiclass() { return bmiclass; } public void setBmiclass(BMICal bmiclass) { this.bmiclass = bmiclass; } public void bmiCalculation() { bmiclass.bmicalculation(weight, height); } public void getInfo() { System.out.println("이름 : " + name); System.out.println("키 : " + height); System.out.println("몸무게 : " + weight); System.out.println("취미 : " + hobby); bmiCalculation(); } }
< BMICal.java >
package com.javalec.ex; import java.util.ArrayList; public class BMICal { private double lowWeight; private double nomal; private double overWeight; private double obesty; 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 > obesty) { System.out.println("비만입니다."); } else if (result > overWeight) { System.out.println("과체중입니다"); }else if (result > nomal) { System.out.println("정상입니다"); } else { System.out.println("저체중입니다"); } } public double getLowWeight() { return lowWeight; } public void setLowWeight(double lowWeight) { this.lowWeight = lowWeight; } public double getNomal() { return nomal; } public void setNomal(double nomal) { this.nomal = nomal; } public double getOverWeight() { return overWeight; } public void setOverWeight(double overWeight) { this.overWeight = overWeight; } public double getObesty() { return obesty; } public void setObesty(double obesty) { this.obesty = obesty; } }
< applicationCTX.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"> <bean id = "bmiCal" class="com.javalec.ex.BMICal"> <property name="lowWeight"> <value>18.5 </value> </property> <property name="nomal"> <value>23</value> </property> <property name="overWeight"> <value>25</value> </property> <property name="obesty"> <value>330</value> </property> </bean> <bean id="myInfo" class="com.javalec.ex.MyInfo"> <property name="name"> <value>홍길동</value> </property> <property name="height"> <value>178</value> </property> <property name="weight"> <value>68</value> </property> <property name="hobby"> <list> <value>운동</value> <value>낮잠</value> </list> </property> <property name="bmiclass"> <ref bean="bmiCal"/> </property> </bean> </beans>
list 타입은 <property> 선언 한 후, <list>로 묶어 <value>를 통해 작성한다.
< MainClass.java >
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(); } }
- 결과물