MapTest.java
package com.sist.di08;
import java.util.Map;
import java.util.Set;
import lombok.Data;
@Data
public class MapTest {
// Map 자료형 선언, 멤버변수
private Map<Integer, String> map;
// 비즈니스 로직
public void prn() {
// keySet() : Map에 있는 키를 전부 가져오는 메소드
Set<Integer> set = map.keySet();
for(Integer k : set) {
System.out.println(map.get(k));
}
}
}
map.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="test" class="com.sist.di08.MapTest">
<property name="map">
<map>
<entry> <!-- map의 하나의 태그 라는 의미 -->
<key> <value>1</value> </key> <!-- 키는 1 -->
<value>짜장면</value> <!-- 값은 '짜장면' -->
</entry>
<entry>
<key> <value>2</value> </key>
<value>짬뽕</value>
</entry>
<entry>
<key> <value>3</value> </key>
<value>탕수육</value>
</entry>
<entry>
<key> <value>4</value> </key>
<value>볶음밥</value>
</entry>
<entry>
<key> <value>5</value> </key>
<value>울면</value>
</entry>
</map>
</property>
</bean>
</beans>
Main.java
package com.sist.di08;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext ctx=
new GenericXmlApplicationContext("classpath:map.xml");
MapTest test = ctx.getBean("test", MapTest.class);
test.prn(); // 비즈니스로직 호출
ctx.close();
}
}
결과
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [map.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@51521cc1: startup date [Mon Dec 13 14:33:56 KST 2021]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6537cf78: defining beans [test]; root of factory hierarchy
짜장면
짬뽕
탕수육
볶음밥
울면
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@51521cc1: startup date [Mon Dec 13 14:33:56 KST 2021]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6537cf78: defining beans [test]; root of factory hierarchy
'Back-End > Spring' 카테고리의 다른 글
[Spring] MVC 패턴 (0) | 2021.12.14 |
---|---|
[Spring] Java 코드를 이용하여 의존관계를 설정하는 방법_애노테이션 방식 (0) | 2021.12.13 |
[Spring] 네임스페이스 방식 (0) | 2021.12.13 |
[Spring] lombok 적용하는 방법, @Data 어노테이션 (0) | 2021.12.13 |
[Spring] 스프링의 개요 / DI(의존성 주입) 하는 방법 2가지 (0) | 2021.12.10 |
댓글