티스토리 뷰
Spring 사용해서 BMI 계산기 만들기
- 이클립스 사용
- 강사가 심각하게 못가르쳐서 유툽에서 강의 찾아보고 공부
<Myinfo>
- 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;
- return 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() {
- bmiCalculation();
- }
- }
-> getter는 안만들어도 됨. setter가 중요. 나는 그냥 같이 만들었다.
<BMICal>
- 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);
- if(result > obesty) {
- } else if (result > overWeight) {
- }else if (result > nomal) {
- } else {
- }
- }
- 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;
- }
- }
<application.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>
-> class명 정확히 작성할것
-> list타입은 <property> 선언 후, <list>로 묶어서 value값 작성
<MainClass>
- package com.javalec.ex;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.GenericXmlApplicationContext;
- public class Mainclass {
- //스프링 컨테이너 구동
- String config = "classpath:application.xml";
- AbstractApplicationContext ctx = new GenericXmlApplicationContext(config); // 스프링 컨테이너 형성
- // 스프링 컨테이너에서 생성한 객체 요청
- MyInfo myinfo = ctx.getBean("myInfo", MyInfo.class); //스프링 컨테이너에서 컴포넌트 가져옴
- myinfo.getInfo();
- //스프링 컨테이너 종료
- ctx.close();
- }
- }
< 결과물 >
'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