본문으로 바로가기

4.DI(Dependency Injection)-II

category 프로그래밍/spring 2017. 11. 22. 22:58

스프링 프로퍼티 설정




그림의 왼쪽을 보시면 xml인 파일입니다. 오른쪽은 Myinfo.class파일이며 setter가 들어있어야 올바르게 실행될 수 있다.

타입이 String이든 , int형이든, double형이든 스프링이 알아서 적절한 데이터 타입으로 값을 할당해준다..

ArrayList<String>은 list타입이므로 xml파일에서도 list로 받는다.

다른 객체를 참조할때는<ref bean ="">



스프링 컨테이너 이해

 


package com.javalec.ex;


public class BMICalculator {

private int obsity;

private int lowweight;

private int overweight;

private int normal;

public void BMICalculation (double height, double weight)

{

double h = 0.01 * height;

double result = weight / (h*h);

System.out.println("BMI지수는" + (int)result + "입니다.");

if(result > obsity) {

System.out.println("비만입니다.");

}

else if(result > overweight) {

System.out.println("과체중입니다.");

}

else if(result > normal) {

System.out.println("정상입니다");

}

else

System.out.println("저체중입니다.");


}

public int getObsity() {

return obsity;

}



public int getLowweight() {

return lowweight;

}



public int getOverweight() {

return overweight;

}



public int getNormal() {

return normal;

}



public void setObsity(int obsity) {

this.obsity = obsity;

}

public void setLowweight(int lowweight) {

this.lowweight = lowweight;

}

public void setOverweight(int overweight) {

this.overweight = overweight;

}

public void setNormal(int normal) {

this.normal = normal;

}

}


package com.javalec.ex;


import java.util.ArrayList;


public class Myinfo {

 private String Name;

 private int weight;

 private int height;

 private ArrayList<String> hobbys;

 BMICalculator bmicalculator;

public String getName() {

return Name;

}

public void setName(String name) {

Name = name;

}

public int getWeight() {

return weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public ArrayList<String> getHobbys() {

return hobbys;

}

public void setHobbys(ArrayList<String> hobbys) {

this.hobbys = hobbys;

}

public BMICalculator getBmicalculator() {

return bmicalculator;

}

public void setBmicalculator(BMICalculator bmicalculator) {

this.bmicalculator = bmicalculator;

}


public void getInfo() {

System.out.println("이름" + Name);

System.out.println("몸무게" + weight );

System.out.println("키" + height);

System.out.println("취미" + hobbys);

bmicalculation();

}

private void bmicalculation() {

// TODO 자동 생성된 메소드 스텁

bmicalculator.BMICalculation(height, weight);

}


 

 

}


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) {

// TODO 자동 생성된 메소드 스텁

String ConfigLocation = "classpath:applicationCTX.xml";

AbstractApplicationContext ctx = new GenericXmlApplicationContext(ConfigLocation);

Myinfo myinfo = ctx.getBean("myinfo",Myinfo.class);

}

}




'프로그래밍 > spring' 카테고리의 다른 글

스프링 컨테이너 생명 주기  (472) 2017.12.07
5.DI활용(의존 관계, di사용에 따른 장점)  (492) 2017.11.26
3. DI(dependency Injection-1)  (1199) 2017.11.22
2.스프링 프로젝트 및 DI ,IDC  (1178) 2017.11.21
1.스프링  (1175) 2017.11.20