2025-01-21




INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContextCollection.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:05:28 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4487af: defining beans [collection]; root of factory hierarchy
홍길동님 점수 : 91
세종대왕님 점수 : 100
유관순님 점수 : 93
김유신님 점수 : 77
신사임당님 점수 : 82
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:05:28 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4487af: defining beans [collection]; root of factory hierarchy
package com.spring.collection;
import java.util.Map;
import java.util.Set;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CollectionUser {
public static void main(String[] args) {
// 스프링 컨테이너를 구동을 하자
GenericXmlApplicationContext container =
new GenericXmlApplicationContext("applicationContextCollection.xml");
// 컨테이너로부터 사용할 객체를 lookup하자
CollectionBean bean = container.getBean("collection", CollectionBean.class);
/* CollectionBean bean = (CollectionBean)container.getBean("collection"); 위와 同*/
Map<String, Integer> addressList = bean.getAddressList();
for (String str : addressList.keySet()) {
System.out.println(str + "님 점수 : " + addressList.get(str));
}
System.out.println();
// 컨테이너를 종료해 주자
container.close();
}
}
package com.spring.collection;
import java.util.Map;
import java.util.Set;
public class CollectionBean {
private Map<String, Integer> addressList;
public Map<String, Integer> getAddressList() {
return addressList;
}
public void setAddressList(Map<String, Integer> addressList) {
this.addressList = addressList;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 스프링 컨테이너가 생성할 객체를 bean으로 등록을 한다. -->
<!-- id : 컨테이너가 생성한 객체들을 식별하기 위해서 사용하는 이름(각각의 고유의 id 지정)
자바의 식별자 작성 규칙이 적용이 됨.
* 숫자로 시작하면 안됨
* 공백을 포함하면 안됨
* 특수기호를 사용하면 안됨
class : 패키지 경로가 포함된 클래스의 이름을 등록을 함.
-->
<!-- 컬렉션에서 배열과 List 자료구조를 이용 시 주입 방법 -->
<bean id="collection" class="com.spring.collection.CollectionBean">
<property name="addressList">
<map>
<entry>
<key><value>홍길동</value></key>
<value>91</value>
</entry>
<entry>
<key><value>세종대왕</value></key>
<value>100</value>
</entry>
<entry>
<key><value>유관순</value></key>
<value>93</value>
</entry>
<entry>
<key><value>김유신</value></key>
<value>77</value>
</entry>
<entry>
<key><value>신사임당</value></key>
<value>82</value>
</entry>
</map>
</property>
</bean>
<!-- 객체의 변경이 잦은 경우는 bean 태그를 이용함. 또한 유지보수 측면에서
변경이 잦지 않은 경우는 애노테이션 사용 -->
</beans>'Spring, Boot > 기초 내용 정리' 카테고리의 다른 글
| Spring(DI)_02_02 (0) | 2025.01.21 |
|---|---|
| Spring(DI)_02_01 (0) | 2025.01.21 |
| Spring(DI)_01_04 (0) | 2025.01.21 |
| Spring(DI)_01_03 (0) | 2025.01.21 |
| Spring(DI)_01_02 (0) | 2025.01.21 |