스프링 4강 - DI 프로퍼티 설정과 컨테이너의 이해
November 07, 2018
4-1 스프링 프로퍼티 설정
- 전 시간에 자세한 주석을 바탕으로 이 코드를 이해해 보자
<!-- 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="bmiCalcaulator" 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="bmiCalcaulator"/>
</property>
</bean>
</beans>
// Main.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();
}
}
4-2 스프링 컨테이너 이해
// Main.java
String configLocation = "classpath:applicationCTX.xml";
// 스프링 컨테이너 생성 (컨테이너에 부품들을 만듬)
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation);
// 스프링 컨테이너에서 컴포넌트 가져옴 (필요한 부품을 가져와서 사용)
MyCalculator myCalculator = ctx.getBean("myInfo", MyInfo.class);
MyInfo.getInfo();
ctx.close();
참고자료
- 스프링과정4강 블스(김명호 강사)