티스토리 뷰

Spring

[Spring] DI 기본예제

먹태 2018. 5. 13. 16:19
Spring 사용해서 BMI 계산기 만들기

- 이클립스 사용
- 강사가 심각하게 못가르쳐서 유툽에서 강의 찾아보고 공부










<Myinfo>

  1. package com.javalec.ex;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class MyInfo {
  6.     private  String name;
  7.     private double height;
  8.     private double weight;
  9.     private ArrayList<String> hobby;
  10.     private BMICal bmiclass;
  11.    
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18.     public double getHeight() {
  19.         return height;
  20.     }
  21.     public void setHeight(double height) {
  22.         this.height = height;
  23.     }
  24.     public double getWeight() {
  25.         return weight;
  26.     }
  27.     public void setWeight(double weight) {
  28.         this.weight = weight;
  29.     }
  30.     public ArrayList<String> getHobby() {
  31.         return hobby;
  32.     }
  33.     public void setHobby(ArrayList<String> hobby) {
  34.         this.hobby = hobby;
  35.     }
  36.     public BMICal getBmiclass() {
  37.         return bmiclass;
  38.     }
  39.     public void setBmiclass(BMICal bmiclass) {
  40.         this.bmiclass = bmiclass;
  41.     }
  42.    
  43.     public void bmiCalculation() {
  44.         bmiclass.bmicalculation(weight, height);
  45.     }
  46.    
  47.     public void getInfo() {
  48.         System.out.println("이름 : " + name);
  49.         System.out.println("키 : " + height);
  50.         System.out.println("몸무게 : " + weight);
  51.         System.out.println("취미 : " + hobby);
  52.         bmiCalculation();
  53.  
  54.     }
  55.    
  56.    
  57. }
  58.  


-> getter는 안만들어도 됨. setter가 중요. 나는 그냥 같이 만들었다.






<BMICal>


  1. package com.javalec.ex;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class BMICal {
  6.         private double lowWeight;
  7.         private double nomal;
  8.         private double overWeight;
  9.         private double obesty;
  10.  
  11.         public void bmicalculation(double weight, double height) {
  12.             double h = height * 0.01 ;
  13.             double result = weight / (h*h);
  14.            
  15.             System.out.println("BMI 지수 :  " + (int)result);
  16.        
  17.             if(result > obesty) {
  18.                     System.out.println("비만입니다.");
  19.             } else if (result > overWeight) {
  20.                 System.out.println("과체중입니다");
  21.             }else if (result > nomal) {
  22.                     System.out.println("정상입니다");
  23.             } else {
  24.                 System.out.println("저체중입니다");
  25.             }
  26.        
  27.         }
  28.  
  29.         public double getLowWeight() {
  30.             return lowWeight;
  31.         }
  32.  
  33.         public void setLowWeight(double lowWeight) {
  34.             this.lowWeight = lowWeight;
  35.         }
  36.  
  37.         public double getNomal() {
  38.             return nomal;
  39.         }
  40.  
  41.         public void setNomal(double nomal) {
  42.             this.nomal = nomal;
  43.         }
  44.  
  45.         public double getOverWeight() {
  46.             return overWeight;
  47.         }
  48.  
  49.         public void setOverWeight(double overWeight) {
  50.             this.overWeight = overWeight;
  51.         }
  52.  
  53.         public double getObesty() {
  54.             return obesty;
  55.         }
  56.  
  57.         public void setObesty(double obesty) {
  58.             this.obesty = obesty;
  59.         }
  60. }
  61.  





<application.xml>


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6.     <bean id = "bmiCal" class="com.javalec.ex.BMICal">
  7.         <property name="lowWeight">
  8.             <value>18.5 </value>
  9.         </property>
  10.         <property name="nomal">
  11.             <value>23</value>
  12.         </property>
  13.         <property name="overWeight">
  14.             <value>25</value>
  15.         </property>
  16.         <property name="obesty">
  17.             <value>330</value>
  18.         </property>
  19.     </bean>
  20.    
  21.     <bean id="myInfo" class="com.javalec.ex.MyInfo">
  22.         <property name="name">
  23.             <value>홍길동</value>
  24.         </property>
  25.         <property name="height">
  26.             <value>178</value>
  27.         </property>
  28.         <property name="weight">
  29.             <value>68</value>
  30.         </property>
  31.         <property name="hobby">
  32.             <list>
  33.                 <value>운동</value>
  34.                 <value>낮잠</value>
  35.             </list>
  36.         </property>
  37.         <property name="bmiclass">
  38.             <ref bean="bmiCal"/>
  39.         </property>
  40.     </bean>
  41. </beans>
  42.  

-> class명 정확히 작성할것

-> list타입은 <property> 선언 후, <list>로 묶어서 value값 작성







<MainClass>


  1. package com.javalec.ex;
  2.  
  3. import org.springframework.context.support.AbstractApplicationContext;
  4. import org.springframework.context.support.GenericXmlApplicationContext;
  5.  
  6. public class Mainclass {
  7.     public static void main(String[] args) {
  8.        
  9.         //스프링 컨테이너 구동
  10.         System.out.println("----- 컨테이너 구동 전 -----");
  11.         String config = "classpath:application.xml";
  12.         AbstractApplicationContext         ctx = new GenericXmlApplicationContext(config);  // 스프링 컨테이너 형성
  13.        
  14.         System.out.println("----- 컨테이너 구동 후 -----");
  15.        
  16.         // 스프링 컨테이너에서 생성한 객체 요청
  17.         MyInfo myinfo = ctx.getBean("myInfo", MyInfo.class)//스프링 컨테이너에서 컴포넌트 가져옴
  18.         myinfo.getInfo();
  19.        
  20.    
  21.         //스프링 컨테이너 종료
  22.         ctx.close();
  23.     }
  24. }
  25.  





< 결과물 >







'Spring' 카테고리의 다른 글

Spring 2  (0) 2020.10.13
Spring 1  (0) 2020.10.13
[Spring] DI Annotation 예제  (0) 2018.05.13
[Spring] DI 기본예제 3  (0) 2018.05.13
[Spring] DI 기본예제 2  (0) 2018.05.13
댓글
최근에 올라온 글
최근에 달린 댓글
링크
Total
Today
Yesterday