[Spring] Spring에서 새터(Setter)로 리스트(List), 맵(Map) 자료구조 주입
| 리스트(List) 및 맵(Map) 자료구조 주입
xml 설정 파일 상에서 새터(Setter) 방식을 이용하여 자바의 리스트(List) 혹은 맵(Map)에 의존성을 주입할 수 있다.
<?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="resturant1" class="com.tutorial.spring.Resturant">
<property name="names">
<list>
<value>Duke</value>
<value>Mario</value>
<value>Suzy</value>
<value>Kate</value>
</list>
</property>
<property name="menus">
<map>
<entry>
<key>
<value>1</value>
</key>
<value>KingCrap</value>
</entry>
<entry>
<key>
<value>2</value>
</key>
<value>IceCream</value>
</entry>
<entry>
<key>
<value>3</value>
</key>
<value>Pork</value>
</entry>
</map>
</property>
</bean>
</beans>
<appContext2.xml 설정파일>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.support.GenericXmlApplicationContext;
class Resturant {
private List<String> names;
private Map<Integer, String> menus;
Resturant(){
names = new ArrayList<>();
menus = new HashMap<>();
}
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public Map<Integer, String> getMenus() {
return menus;
}
public void setMenus(Map<Integer, String> menus) {
this.menus = menus;
}
}
public class App {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:appContext2.xml");
Resturant resturant = ctx.getBean("resturant1", Resturant.class);
System.out.println("--- names property ---");
resturant.getNames().stream().forEach(System.out::println);
System.out.println("--- menus property ---");
resturant.getMenus().forEach((k, v) -> {
System.out.println("key : " + k + " " + "value : " + v);
});
}
}
11월 28, 2018 11:15:32 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
정보: Loading XML bean definitions from class path resource [appContext2.xml]
11월 28, 2018 11:15:32 오후 org.springframework.context.support.AbstractApplicationContext prepareRefresh
정보: Refreshing org.springframework.context.support.GenericXmlApplicationContext@439f5b3d: startup date [Wed Nov 28 23:15:32 KST 2018]; root of context hierarchy
11월 28, 2018 11:15:32 오후 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@59a6e353: defining beans [resturant1]; root of factory hierarchy
--- names property ---
Duke
Mario
Suzy
Kate
--- menus property ---
key : 1 value : KingCrap
key : 2 value : IceCream
key : 3 value : Pork
'Spring Framework > Spring 입문 - 개념 및 핵심' 카테고리의 다른 글
[Spring] 어노테이션(Annotation)을 이용한 스프링 설정 (0) | 2020.12.21 |
---|---|
[Spring] 스프링 컨테이너(Spring Container)와 빈(Bean) 객체 생명 주기 및 init-method, destroy-method, @PostConstruct, @PreDestroy (0) | 2020.12.17 |
[Spring] 스프링(Spring) @Qualifier, @Named, @Primary 의존객체 선택 (0) | 2020.12.17 |
[Spring] 의존객체 자동 주입(Automatic Dependency Injection), @Autowired, @Resource, @Inject (0) | 2020.12.17 |
[Spring] 스프링 빈 범위 지정(Spring Bean Scope), 싱글턴(Singleton), 프로토타입(Prototype) (0) | 2020.12.17 |
[Spring] 여러개로 나뉜 Spring XML 설정파일 합치기 (0) | 2020.12.17 |
[Spring] 의존성 주입, DI(Dpendency Injection), 스프링(Spring) 의존성 주입 (0) | 2020.12.17 |
[Spring] 스프링 프레임워크(Spring Framework)란? (0) | 2020.12.17 |